Adding a new column to existing DataFrame in Pandas in Python


In this tutorial, we are going to learn how to add a new column to the existing DataFrame in pandas. We can have different methods to add a new column. Let's all of them.

Using List

We can add a new column using the list. Follow the steps to add a new column.

Algorithm

1. Create DataFrame using a dictionary.
2. Create a list containing new column data. Make sure that the length of the list matches the length of the data which is already present in the data frame.
3. Add the list to the DataFrame like dictionary element.

Let's see one example.

Example

# importing pandas
import pandas as pd

# creating a DataFrame
data = {
   'Name': ['Hafeez', 'Aslan', 'Kareem'],
   'Age': [19, 18, 15],
   'Profession': ['Pythoneer', 'Programmer', 'Student']
}
dataframe = pd.DataFrame(data)
print('-----------Before adding a new column----------')
print(dataframe)
print('\n\n')

# creating a list for new column
places = ['Nellore', 'Mumbai', 'Andhra']

# we are using 'Places' as column name
# adding the list to the dataframe as column
dataframe['Places'] = places
print('---------------After adding a new column------------')
print(dataframe)

Output

If you run the above program you will get the following results.

-----------Before adding a new column----------
    Name     Age   Profession
0  Hafeez    19     Pythoneer
1   Aslan    18    Programmer
2  Kareem    15       Student
---------------After adding a new column------------
    Name   Age    Profession    Places
0  Hafeez   19    Pythoneer    Nellore
1   Aslan   18   Programmer     Mumbai
2 K areem   15      Student     Andhra

DataFrame.insert()

There is a built-in method called insert() to add a new column. Steps to follow.

Algorithm

1. Create DataFrame using dictionary.
2. Create a list containing new column data. Make sure that the length of the list matches the length of the data which is already present in the data frame.
3. Insert the data into the DataFrame using DataFrame.insert(index, column_name, data)
method.

Example

# importing pandas
import pandas as pd

# creating a DataFrame
data = {
   'Name': ['Hafeez', 'Aslan', 'Kareem'],
   'Age': [19, 18, 15],
   'Profession': ['Pythoneer', 'Programmer', 'Student']
}
dataframe = pd.DataFrame(data)
print('-----------Before adding a new column----------')
print(dataframe)
print('\n\n')

# creating a list for new column
places = ['Nellore', 'Mumbai', 'Andhra']

# we are using 'Places' as column name
# adding the list to the dataframe as column using insert(index, column_name, data)
dataframe.insert(2, 'Places', places)
print('---------------After adding a new column------------')
print(dataframe)

Output

If you run the above program, you will get the following results.

-----------Before adding a new column----------
      Name  Age  Profession
0   Hafeez   19   Pythoneer
1    Aslan   18  Programmer
2   Kareem   15     Student

---------------After adding a new column------------
     Name  Age   Places  Profession
0  Hafeez   19  Nellore   Pythoneer
1   Aslan   18   Mumbai  Programmer
2  Kareem   15   Andhra     Student

DataFrame.assign()

This method takes one argument i.e., a list of data and adds it to the data frame as a column at the end.

Algorithm

1. Create DataFrame using a dictionary.
2. Create a list containing new column data. Make sure that the length of the list matches the length of the data which is already present in the data frame.
3. Insert the data into the DataFrame using DataFrame.assign(column_name = data)
method. It returns a new data frame. So, we have to store it.
4. Print the new data frame.

Let's see an example.

Example

# importing pandas
import pandas as pd
# creating a DataFrame
data = {
   'Name': ['Hafeez', 'Aslan', 'Kareem'],
   'Age': [19, 18, 15],
   'Profession': ['Pythoneer', 'Programmer', 'Student']
}
dataframe = pd.DataFrame(data)
print('-----------Before adding a new column----------')
print(dataframe)
print('\n\n')
# creating a list for new column
places = ['Nellore', 'Mumbai', 'Andhra']
# we are using 'Places' as column name
# adding the list to the dataframe as column using assign(column_name = data)
new_dataframe = dataframe.assign(Places = places)
print('---------------After adding a new column------------')
print(new_dataframe)

Output

If you run the above program, you will get the following results.

-----------Before adding a new column----------
   Name     Age     Profession
0  Hafeez   19       Pythoneer
1   Aslan   18      Programmer
2  Kareem   15         Student
---------------After adding a new column------------
     Name    Age    Profession    Places
0  Hafeez    19      Pythoneer   Nellore
1   Aslan    18     Programmer    Mumbai
2  Kareem    15        Student    Andhra

Conclusion

If you have any doubts regarding the tutorial, mention them in the comment section.

Updated on: 26-Aug-2023

27K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements