Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 an existing DataFrame in pandas. There are several methods to accomplish this task, each with its own advantages depending on your specific needs.
Using Direct Assignment
The simplest way to add a new column is by using direct assignment with a list. This method assigns the new column data to the DataFrame like a dictionary element ?
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')
# creating a list for new column
places = ['Nellore', 'Mumbai', 'Andhra']
# adding the list to the dataframe as column
dataframe['Places'] = places
print('---------------After adding a new column------------')
print(dataframe)
Output
-----------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
Using DataFrame.insert()
The insert() method allows you to add a new column at a specific position in the DataFrame. This is useful when you need to control the column order ?
Syntax
DataFrame.insert(loc, column, value)
Where loc is the insertion index, column is the column name, and value is the column data.
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')
# creating a list for new column
places = ['Nellore', 'Mumbai', 'Andhra']
# inserting the column at index 2 (between Age and Profession)
dataframe.insert(2, 'Places', places)
print('---------------After adding a new column------------')
print(dataframe)
Output
-----------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
Using DataFrame.assign()
The assign() method creates a new DataFrame with the additional column, leaving the original DataFrame unchanged. This method is useful for functional programming approaches ?
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')
# creating a list for new column
places = ['Nellore', 'Mumbai', 'Andhra']
# adding the list to the dataframe as column using assign()
new_dataframe = dataframe.assign(Places=places)
print('---------------After adding a new column------------')
print(new_dataframe)
Output
-----------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
Comparison of Methods
| Method | Modifies Original | Position Control | Best For |
|---|---|---|---|
| Direct Assignment | Yes | End only | Simple column addition |
insert() |
Yes | Any position | Specific column placement |
assign() |
No | End only | Functional programming |
Conclusion
Use direct assignment for simple column additions, insert() when you need to control column position, and assign() for immutable operations. All methods require that the new column data length matches the DataFrame's row count.
