Adding New Column to Existing DataFrame in Pandas
Last Updated :
08 Nov, 2024
Let’s discuss how to add new columns to the existing DataFrame in Pandas. There can be multiple methods to add new column or multiple columns to existing dataframe, based on different requirement.
By Declaring a new List as column
Let’s first create a dataframe. We will keep using this dataframe for all examples in this article.
Python
import pandas as pd
# Define a dictionary containing Students data
data = {'Name': ['Pandas', 'Geeks', 'for', 'Geeks'],
'Height': [1, 2, 3, 4],
'Qualification': ['A', 'B', 'C', 'D']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
We can simply add new column using a list. Note that the length of your list should match the length of index column, else it will show an error.
Python
# Declare a list that is to be converted into a column
address = ['NewYork', 'Chicago', 'Boston', 'Miami']
# Using 'Address' as the column name
# and equating it to the list
df['Address'] = address
print(df)
Note: Using list for adding column will modify the existing dataframe.
By using DataFrame.assign() method
assign() method creates a new DataFrame with the specified column(s) added. The original DataFrame remains unchanged unless we explicitly reassign the result back to it.
Python
import pandas as pd
# Define a dictionary containing Students data
data = {'Name': ['Pandas', 'Geeks', 'for', 'Geeks'],
'Height': [1, 2, 3, 4],
'Qualification': ['A', 'B', 'C', 'D']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# using assign() and passing address list as parameter
df2 = df.assign(address = ['NewYork', 'Chicago', 'Boston', 'Miami'])
print(df2)
Output Name Height Qualification address
0 Pandas 1 A NewYork
1 Geeks 2 B Chicago
2 for 3 C Boston
3 Geeks 4 D Miami
By using Dictionary
We can use a Python dictionary to add a new column in pandas DataFrame. This method is recommended when you want to add multiple columns at once or if you have columns in a dictionary format.
Python
import pandas as pd
# Define a dictionary containing Students data
data = {'Name': ['Pandas', 'Geeks', 'for', 'Geeks'],
'Height': [1, 2, 3, 4],
'Qualification': ['A', 'B', 'C', 'D']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# address dictionary with names as keys & addresses as values
address = {'Pandas': 'NewYork', 'Geeks': 'Chicago',
'for': 'Boston', 'Geeks_2': 'Miami'}
# Add the 'Address' column by mapping the 'Name' column
# to the address dictionary
df['Address'] = df['Name'].map(address)
print(df)
Output Name Height Qualification Address
0 Pandas 1 A NewYork
1 Geeks 2 B Chicago
2 for 3 C Boston
3 Geeks 4 D Chicago
Adding More than One columns in Existing Dataframe
Using this method with assign() is better, as assign() accepts multiple key-value pairs and can use dictionary unpacking to add multiple columns in a single step.
Python
#example - add both "Age" and "City" columns
new_columns = {'Age': [21, 22, 23, 24],
'City': ['NY', 'LA', 'SF', 'DC']}
# unpacks new_columns dictionary and adds both "Age" and "City"
df = df.assign(**new_columns)
Check out more ways to add multiple columns in Pandas Dataframe.
By using DataFrame.insert()
It gives the freedom to add a column at any position and not just at the end. It also provides different options for inserting the column values.
Python
import pandas as pd
# Define a dictionary containing Students data
data = {'Name': ['Pandas', 'Geeks', 'for', 'Geeks'],
'Height': [1, 2, 3, 4],
'Qualification': ['A', 'B', 'C', 'D']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# Using insert() to add a column at position 2 (3rd column)
df.insert(2, "Age", [21, 23, 24, 21], True)
# Observe the result
print(df)
insert() method modifies the original dataframe, so thereâs no need to reassign the DataFrame after using it.
By using Dataframe.loc()
Using .loc[], you can add a new column directly or modify values based on conditions, or when adding new columns based on specific row selections.
Python
import pandas as pd
# dictionary containing Students data
data = {'Name': ['Pandas', 'Geeks', 'for', 'Geeks'],
'Height': [1, 2, 3, 4],
'Qualification': ['A', 'B', 'C', 'D']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# Using .loc[] to add a "Category" column
# based on height condition
df.loc[df['Height'] >= 3, 'Category'] = 'Tall'
df.loc[df['Height'] < 3, 'Category'] = 'Short'
# Observe the result
print(df)
Output Name Height Qualification Category
0 Pandas 1 A Short
1 Geeks 2 B Short
2 for 3 C Tall
3 Geeks 4 D Tall
FAQs on Add new column to existing DataFrame in Pandas
How to create new columns derived from existing columns?
Use assignment ( = ) with the DataFrame indexing ( [] ) to create new columns based on existing ones.
import pandas as pd
# Creating a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Creating a new column 'C' derived from 'A' and 'B'
df['C'] = df['A'] + df['B']
Here, df[‘C’] = df[‘A’] + df[‘B’] creates a new column ‘C’ in DataFrame df by adding columns ‘A’ and ‘B’.
How to add columns in pandas?
You can add columns by directly assigning values or using DataFrame methods like insert() or assign() .
import pandas as pd
# Creating a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Adding a new column 'C'
df['C'] = [7, 8, 9]
Here, df['C'] = [7, 8, 9] adds a new column ‘C’ to DataFrame df with the specified values.
What are the steps to add column names in pandas?
Column names can be added during DataFrame creation or by assigning to the columns attribute of an existing DataFrame.
import pandas as pd
# Creating a DataFrame with specified column names
df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
How to add columns to a pandas DataFrame from another DataFrame?
Use pd.concat() or the DataFrame’s join() or merge() methods to add columns from another DataFrame.
import pandas as pd
# Creating two DataFrames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'C': [7, 8, 9]})
# Adding columns from df2 to df1
df1['C'] = df2['C']
Here, df1['C'] = df2['C'] adds column ‘C’ from df2 to df1 .
How to add columns to a pandas DataFrame from a list?
Simply assign the list to a new column name in the DataFrame.
import pandas as pd
# Creating a DataFrame
df = pd.DataFrame({'A': [1, 2, 3]})
# Adding a new column 'B' from a list
new_column = [4, 5, 6]
df['B'] = new_column
Here, df['B'] = new_column adds a new column ‘B’ to DataFrame df with values from new_column .