Pandas simplify many tedious, time-consuming tasks associated with working with the data. The columns in the DataFrame can also be adjusted, along with the data source. There are four ways to add a column to a DataFrame in Pandas, but in this article, we use the Pandas column “insert()” function.
DataFrame.insert()
By utilizing the DataFrame “insert()” method, you can add columns between the current columns rather than adding them at the bottom of the Pandas DataFrame. It allows us to add a column anywhere we choose rather than simply at the conclusion. Additionally, it offers many ways to add the values for the columns. When you add a column at a specified position or index, the Pandas “insert()” function is useful.
Syntax
Parameters
- “position” is the first parameter that refers to the column index position where the column has to be inserted.
- “column” is the new column name
- Values are placed in a list and inserted into the column.
Example 1
In this example, we have a DataFrame named “things” that holds the “Name” and “Purchased Status” columns.
Let’s add a new column named “Cost” with values.
things=pandas.DataFrame({'Name':['Solar dish','glasses','oil'],
'Purchased Status':[1,0,0]})
print(things)
# Insert 'Cost' column to the above dataframe at index-2
things.insert(2, "Cost", [890.0,98.0,67.91])
print()
print(things)
Output
0 Solar dish 1
1 glasses 0
2 oil 0
Name Purchased Status Cost
0 Solar dish 1 890.00
1 glasses 0 98.00
2 oil 0 67.91
Explanation
We specified the position as 2 in the insert() function. So the column “Cost” is inserted in the third position (index – 2). Finally, the columns are [‘Name’,‘Purchased Status’,‘Cost’].
Example 2
Let’s add a new column named “review” with string-type values at position-1.
things=pandas.DataFrame({'Name':['Solar dish','glasses','oil'],
'Purchased Status':[1,0,0]})
print(things)
# Insert 'review' column to the above dataframe at index-1
things.insert(1, "review",["good","bad","good"])
print()
print(things)
Output
0 Solar dish 1
1 glasses 0
2 oil 0
Name review Purchased Status
0 Solar dish good 1
1 glasses bad 0
2 oil good 0
Explanation
We specified the position as 1 in the insert() function. So the column “review” is inserted in the second position (index-1). Finally, the columns are [‘Name’,‘review’,‘Purchased Status’].
Example 3
Create a DataFrame named orders with 2 columns and insert 2 columns at index-1 one by one.
orders=pandas.DataFrame({'id':[1,2,3,4,5],
'name':['o1','o2','o3','o4','o5']})
print(orders)
# Insert 'Company' column to the above dataframe at index-1.
orders.insert(1, "Company", ["comp-1","comp-2","comp-3","comp-4","comp-5"])
print()
print(orders)
# Insert 'Sales' column to the above dataframe at index-1.
orders.insert(1, "Sales", [10,20,30,56,78])
print()
print(orders)
Output
0 1 o1
1 2 o2
2 3 o3
3 4 o4
4 5 o5
id Company name
0 1 comp-1 o1
1 2 comp-2 o2
2 3 comp-3 o3
3 4 comp-4 o4
4 5 comp-5 o5
id Sales Company name
0 1 10 comp-1 o1
1 2 20 comp-2 o2
2 3 30 comp-3 o3
3 4 56 comp-4 o4
4 5 78 comp-5 o5
Explanation
First, the order of the columns is [id,name].
After adding ‘Company’ at position 1, the columns are [id.Company,name].
After adding ‘Sales’ at position 1, the columns are [id.Sales,Company,name].
Conclusion
A commonly used data analysis and update operation is adding columns to DataFrame. Pandas gives you numerous options for completing the task by offering four different methods. However, in our article, we only utilize one technique, which is the Pandas “insert()” column. We discussed three different examples of inserting the column in an existing DataFrame.