3 Examples to Add Column in a Pandas Data Frame

How to add a column in an existing data frame using Pandas?

In this tutorial, we will show you different ways of adding columns in the Pandas data frame.

For the examples, we have the following data frame created:

Code:

import pandas as pd


inflation_data = {

"Year": [2015, 2016, 2016, 2017],

"Inflation": ['1.75%', '2%', '3%', '3.5%']

}


#Creating a Data frame

df = pd.DataFrame(inflation_data)


#Display Data Frame

print(df)

Output:

   Year Inflation

0  2015     3.25%

1  2016     2.15%

2  2016        3%

3  2017      1.5%

Using the insert method of the data frame

The insert is a data frame method that is used to add a column in the specified position.

A duplicate column name can also be added if allow_duplicate is set to True.

See the example below where we will add a new column (“Difference”) in the above-created data frame.

Code:

import pandas as pd

inflation_data = {

"Year": [2015, 2016, 2016, 2017],

"Inflation": ['1.75%', '2%', '3%', '3.5%']

}

#Creating a Data frame

df = pd.DataFrame(inflation_data)

df.insert(1, "Difference", [0.75, 0.25,1,0.5])


#Display DF after adding a column

print(df)

Output:

Year  Difference Inflation

0  2015        0.75     1.75%

1  2016        0.25        2%

2  2016        1.00        3%

3  2017        0.50      3.5%

How did it work?

  • We want to add a new column with the name “Difference” in the DF.
  • In the insert method, we specified position 1 (0-based index) for the “Difference” column.
  • By using insert, you may also provide data. We provided data in the list.

Second Way – want to add a column at the end of the existing Data Frame?

This is the easiest way of adding a new column along with data in the existing data frame. However, the drawback is, it can add a column at the end.

For that, simply use the DF name along with the new column and assign the values in a list – that’s it.

Have a look at the example below where we will add the “Difference” column in the above-created DF.

Code:

import pandas as pd

inflation_data = {

"Year": [2015, 2016, 2016, 2017],

"Inflation": ['1.75%', '2%', '3%', '3.5%']

}


#Creating a Data frame

df = pd.DataFrame(inflation_data)


#Adding a new column with values

df["Difference"] = [0.75, 0.25,1,0.5]


#Display DF after adding a column

print(df)

Result:

pandas-df-add-column

You can see the newly added column is placed at the end of DF.

Third way – Using a dictionary/map for adding a column

You may also use a Python dictionary and map combination for adding a new column in the existing data frame.

Code:

import pandas as pd

#Creating a dictionary


tel_dir = {'Name': ['Mike','Haynes','Mina'],

'Phone No':[123445689,45678910, 635363636]}


#Create data frame based on dict

df_dir = pd.DataFrame.from_dict(tel_dir)

email = {'Mike':'mike@abc.com','Haynes':'haynes@abc.com','Mina':'mina@abc.com'}

#Adding a column to DF by passing dict to map

df_dir['Email'] = df_dir['Name'].map(email)


#Display data frame

print(df_dir)

Output:

Name   Phone No           Email

0    Mike  123445689    mike@abc.com

1  Haynes   45678910  haynes@abc.com

2    Mina  635363636    mina@abc.com

How did it work?

  • In that case, we passed the dictionary to the map
  • It will perform a lookup and return the associated value for that key.
  • Alternatively, we used a column from the existing DF and passed a new column’s value to it.

Conclusion:

Due to its flexibility (for allowing adding columns at any position), we may categorize Data Frame’s insert method as the better way for adding columns in the data frame.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!