Pandas Data frame is a two-dimensional data structure that stores data in rows and columns structure.
You can add column to pandas dataframe using the df.insert(col_index_position, “Col_Name”, Col_Values_As_List, True) statement.
In this tutorial, you’ll see different methods available to add columns to pandas dataframe.
If you’re in Hurry
You can use the below code snippet to add a new column to the pandas dataframe.
To add a column with empty values
df["new_Column"] = pd.NaT
df
To add a column with values
new_column_values = ['val1','val2','val3','val4','val5']
df["new_Column"] = new_column_values
df
This is how you can add a new column to the pandas dataframe.
If You Want to Understand Details, Read on…
In this tutorial, you’ll learn the different methods available to add columns to the pandas dataframe. You can add columns using
- Assignment operator or the subscript notation – Use the assignment operator = to create a column in the dataframe and assign list of values.
- dataframe.insert() method – Use
insert()
method when you want to insert a column in a specific index position of the dataframe. - datafame.assign() method – Use
assign()
method when you want to insert a column and create a new dataframe out of it rather inserting a new column in the same dataframe.
Table of Contents
Sample Dataframe
This is the sample dataframe used throughout the tutorial.
import pandas as pd
data = {"product_name":["Keyboard","Mouse", "Monitor", "CPU", "Speakers"],
"Unit_Price":[500,200, 5000, 10000, 250],
"No_Of_Units":[5,5, 10, 20, 8],
}
df = pd.DataFrame(data)
df
Dataframe Looks Like
product_name | Unit_Price | No_Of_Units | |
---|---|---|---|
0 | Keyboard | 500 | 5 |
1 | Mouse | 200 | 5 |
2 | Monitor | 5000 | 10 |
3 | CPU | 10000 | 20 |
4 | Speakers | 250 | 8 |
Let’s see the different types of adding a column to pandas dataframe.
Using Subscript Notation or Assignment operator**
You can add a column by using the =
operator with a list of values.
The length of the list of values must be equal to the length of the rows in the dataframe. Otherwise, an error
will be raised.
list = ['val1','val2','val3','val4','val5']
df["new_column"] = list
where,
list = ['val1','val2','val3','val4','val5']
– creating a list with valuesdf["new_column"] = list
– assigning the list to the dataframe column called “new_column”.
When you execute the below code snippet, a new column called Tax_new will be added to the dataframe with values available in the list called as tax
.
tax = [10,15,12,10,11]
df['Tax_new %'] = tax
df
Dataframe Looks Like
product_name | Unit_Price | No_Of_Units | Total_Price | Tax_new % | |
---|---|---|---|---|---|
0 | Keyboard | 500 | 5 | NaT | 10 |
1 | Mouse | 200 | 5 | NaT | 15 |
2 | Monitor | 5000 | 10 | NaT | 12 |
3 | CPU | 10000 | 20 | NaT | 10 |
4 | Speakers | 250 | 8 | NaT | 11 |
Using Insert() method
You can add a column to pandas dataframe using the insert() method available in the pandas dataframe.
Usage
- When you want to insert a column in specific position
- To avoid inserting duplicate columns with the same name. You can avoid duplicates by specifying
allow_duplicates
flag.
Below is the code snippet to add column using the insert()
method.
# Using DataFrame.insert() to add a column
df.insert(3, "Tax%", [5,10,10,5,10], True)
df
where,
3
– Position where the new column needs to be insertedTax%
– Name of the new column[5,10,10,5,10]
– List of values to be assigned to the new columnTrue
– To allow duplicate columns. IfFalse
, the new column will not be inserted if a column with name Tax% is already existing.
Dataframe Looks Like
product_name | Unit_Price | No_Of_Units | Tax% | Total_Price | Tax_new % | |
---|---|---|---|---|---|---|
0 | Keyboard | 500 | 5 | 5 | NaT | 10 |
1 | Mouse | 200 | 5 | 10 | NaT | 15 |
2 | Monitor | 5000 | 10 | 10 | NaT | 12 |
3 | CPU | 10000 | 20 | 5 | NaT | 10 |
4 | Speakers | 250 | 8 | 10 | NaT | 11 |
Using Assign() method
You can add a column to the pandas dataframe using the assign() method.
Usage
- When you want to create a new dataframe with the existing dataframe with additional new columns inserted.
- If you want to avoid modifications in the original dataframe.
The following code demonstrates how to use the assign()
method.
df2 = df.assign(Remarks = pd.NaT)
df2
Where,
Remarks = pd.NaT
– Remarks is the column name to be inserted.pd.Nat
is the values to be assigned to the new column. Note that, the column name is not enclosed with single quotes or double quotes.
Dataframe Looks Like
product_name | Unit_Price | No_Of_Units | Tax% | Total_Price | Tax_new % | Remarks | |
---|---|---|---|---|---|---|---|
0 | Keyboard | 500 | 5 | 5 | NaT | 10 | NaT |
1 | Mouse | 200 | 5 | 10 | NaT | 15 | NaT |
2 | Monitor | 5000 | 10 | 10 | NaT | 12 | NaT |
3 | CPU | 10000 | 20 | 5 | NaT | 10 | NaT |
4 | Speakers | 250 | 8 | 10 | NaT | 11 | NaT |
Add column At Specific Index
In this section, you’ll add a column at a specific position.
- Add a column at a specific index by using the
df.insert()
method.
Code
Use the below snippet to add a column at a specific index.
# Using DataFrame.insert() to add a column
df.insert(3, "State Tax", [5,10,10,5,10], True)
df
where,
3
– Position where the new column needs to be insertedState Tax
– Name of the new column[5,10,10,5,10]
– List of values to be assigned to the new columnTrue
– To allow duplicate columns. IfFalse
, the new column will not be inserted if a column with name Tax% is already existing.
An index is zero-based. Hence you’ll see the new column State Tax added in the fourth position of the dataframe.
Dataframe Looks Like
product_name | Unit_Price | No_Of_Units | State Tax | Tax% | Total_Price | Tax_new % | |
---|---|---|---|---|---|---|---|
0 | Keyboard | 500 | 5 | 5 | 5 | NaT | 10 |
1 | Mouse | 200 | 5 | 10 | 10 | NaT | 15 |
2 | Monitor | 5000 | 10 | 10 | 10 | NaT | 12 |
3 | CPU | 10000 | 20 | 5 | 5 | NaT | 10 |
4 | Speakers | 250 | 8 | 10 | 10 | NaT | 11 |
Dataframe Will Look like
product_name | Unit_Price | No_Of_Units | Tax% | Total_Price | |
---|---|---|---|---|---|
0 | Keyboard | 500 | 5 | 5 | NaT |
1 | Mouse | 200 | 5 | 10 | NaT |
2 | Monitor | 5000 | 10 | 10 | NaT |
3 | CPU | 10000 | 20 | 5 | NaT |
4 | Speakers | 250 | 8 | 10 | NaT |
You’ve learned how to add columns at a specific indexes.
Add Column to Dataframe With Constant Value
In this section, you’ll learn how to add a column to a dataframe with a constant value.
- You can do this by assigning a single value using the assignment operator as shown below.
df["Price_Increase_Col"] = 200
df
Where,
df["Price_Increase_Col"]
– specifying the new column in the dataframe.200
– Constant value to be added to all the cells in the new column.
Dataframe Will Look Like
Now, a new column called Price_Increase_Col will be added to the dataframe with the value 200 in all the cells.
product_name | Unit_Price | No_Of_Units | Tax% | Total_Price | Price_Increase_Col | |
---|---|---|---|---|---|---|
0 | Keyboard | 500 | 5 | 5 | NaT | 200 |
1 | Mouse | 200 | 5 | 10 | NaT | 200 |
2 | Monitor | 5000 | 10 | 10 | NaT | 200 |
3 | CPU | 10000 | 20 | 5 | NaT | 200 |
4 | Speakers | 250 | 8 | 10 | NaT | 200 |
Add Multiple Columns to Dataframe
You can add multiple columns to the dataframe by using the assignment operator.
Code
df['new_column_1'], df['new_column_2'] = [constant_value_for_Col_1, constant_value_for_Col_2]
df
All the cells of the column will have the same value.
Example
You’re adding two columns Product_Category and Available_Units to the dataframe df
.
df['Product_Category'], df['Available_Units'] = [pd.NaT, 3]
df
Where,
df['Product_Category'], df['Available_Units']
– List of new columns to be added separated by comma.[pd.NaT, 3]
– List of constant values to be added as a default value for the newly added column respectively.
Now, two new columns are added to the dataframe.
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Tax% | Total_Price | Price_Increase_Col | Product_Category | Availabile_Units | |
---|---|---|---|---|---|---|---|---|
0 | Keyboard | 500 | 5 | 5 | NaT | 200 | NaT | 3 |
1 | Mouse | 200 | 5 | 10 | NaT | 200 | NaT | 3 |
2 | Monitor | 5000 | 10 | 10 | NaT | 200 | NaT | 3 |
3 | CPU | 10000 | 20 | 5 | NaT | 200 | NaT | 3 |
4 | Speakers | 250 | 8 | 10 | NaT | 200 | NaT | 3 |
You’ve learned how to append multiple columns to the dataframe at once.
You May also Like
- How to rename column in pandas
- How to Drop Column in pandas dataframe