# Pandas – Add a new column to an existing DataFrame.

In this post, you will learn various ways to add a new column to an existing dataframe.

Let’s create a pandas dataframe to work with.

data = {'Restaurant': [1, 2, 3, 4, 5],
'Quality rating': ['Good','Very Good','Good','Excellent','Very Good'],
'Meal price (\$)': [18, 22, 28, 38, 33]}

df = pd.DataFrame(data)
df

Now, suppose you want to add the location of the restaurants. How would you do that?

Think the DataFrame as a dictionary in python. The key are the column names and values are the values of the column. Now, you already know how to add a key-value pair in python. Just apply the same logic.

df['Location'] = ['Pune','Mumbai','New Delhi','Banglore','Chennai']
df

This is one of the easiest way to add a new column column to a dataframe.

By default this method adds the new column at the end. But If you want to insert a column in any particular place, you can use the DataFrame.insert() method. let’s drop the column and try to add it after quality rating column.

# drop the column first
df.drop('Location', axis=1, inplace=True)
# add the column using insert method
df.insert(2, 'Location', ['Pune','Mumbai','New Delhi','Banglore','Chennai'])
df

Rating: 1 out of 5.