Empty columns are added to the pandas dataframe as a placeholder to add values at a later point in time.
You can add an empty column to pandas dataframe using df["Empty_Column"] = None
statement.
In this tutorial, you’ll learn different methods to add empty columns to pandas dataframe.
If you’re in Hurry
You can use the below code snippet to add an empty column to the pandas dataframe.
df["Empty_Column"] = " "
df
If You Want to Understand Details, Read on…
Let’s look into the detail of adding a blank column to pandas dataframe in a detailed way.
Table of Contents
Sample Dataframe
Create an empty dataframe and append two rows.
This sample dataframe is used to demonstrate adding blank columns to the dataframe.
Snippet
import pandas as pd
dict = {'First Name': 'Vikram', 'Last Name': 'Aruchamy', 'Country': 'India'}
df = pd.DataFrame()
#Adding first row
df = df.append(dict, ignore_index = True)
#Adding second row using the same dictionary
df = df.append(dict, ignore_index = True)
print(df)
Dataframe Will Look Like
Country First Name Last Name
0 India Vikram Aruchamy
1 India Vikram Aruchamy
Now, let’s add an empty column to this dataframe.
Add Empty Column to Pandas
You can add an empty column to the pandas dataframe using the =
operator and assign null
values to the column.
Code
df["Empty_Column"] = " "
df
Dataframe Will Look Like
An empty column is added at the end of the dataframe with the column header Empty_Column.
Country | First Name | Last Name | Empty_Column | |
---|---|---|---|---|
0 | India | Vikram | Aruchamy | |
1 | India | Vikram | Aruchamy |
You can also add a column with nan values. nan
value equals empty or blank values, which is used to denote the missing values in pandas. The nan
value is available in the Numpy package.
Add Empty Column Using NP.nan
You can add a column with np.nan
to create a blank column with NaN
values, as shown below.
Code
import numpy as np
df["NaN_Column"] = np.nan
df
Dataframe Will Look Like
Country | First Name | Last Name | Empty_Column | NaN_Column | |
---|---|---|---|---|---|
0 | India | Vikram | Aruchamy | NaN | |
1 | India | Vikram | Aruchamy | NaN |
Add Empty Column Using None
You can also use None
to create empty or blank columns in the dataframe, as shown below.
Code
df["None_Column"] = None
df
Dataframe Will Look Like
Country | First Name | Last Name | Empty_Column | NaN_Column | None_Column | |
---|---|---|---|---|---|---|
0 | India | Vikram | Aruchamy | NaN | None | |
1 | India | Vikram | Aruchamy | NaN | None |
This is how you can add a single empty or blank column to the pandas dataframe.
Add Multiple Empty Columns to Pandas Dataframe
In this section, you’ll learn how to add multiple empty columns to the pandas dataframe at once.
- Use the reindex() method to add multiple columns.
The reindex()
method conforms the dataframe to a new index as specified. When adding a new column, the reindex method conforms the dataframe to the index with new columns and returns a new dataframe with the changed index.
To add columns using reindex()
method,
- Get the list of existing columns in the dataframe using
df.columns.tolist()
- Add additional columns to the list.
- The newly added columns will have
NaN
values by default to denote the missing values. - Assign this new list to the columns attribute of the dataframe in the
reindex()
method, as shown below.
Snippet
df = df.reindex(columns = df.columns.tolist() + ["new_column_1", "new_column_2"])
df
Dataframe Will Look Like
Country | First Name | Last Name | Empty_Column | NaN_Column | None_Column | new_column_1 | new_column_2 | |
---|---|---|---|---|---|---|---|---|
0 | India | Vikram | Aruchamy | NaN | None | NaN | NaN | |
1 | India | Vikram | Aruchamy | NaN | None | NaN | NaN |
This is how you can add multiple empty columns at once.
Add Empty Column If Not Exists
In some cases, you may need to add an empty column ONLY if it does not exist in the dataframe.
To add empty column if doesn’t exist,
- check if the column_name already exists in the columns list by using
If column_name not in df.columns
. - Add the column to the dataframe if it doesn’t exist. Else print an error message.
Code
Use the following code to add an empty column named new_column_3
if it doesn’t exist in the dataframe.
if 'new_column_3' not in df.columns:
df["new_column_3"] = " "
else:
print("new_column_1 already exists in the dataframe")
df
Dataframe Will Look Like
Country | First Name | Last Name | Empty_Column | NaN_Column | None_Column | new_column_1 | new_column_2 | new_column_3 | |
---|---|---|---|---|---|---|---|---|---|
0 | India | Vikram | Aruchamy | NaN | None | NaN | NaN | ||
1 | India | Vikram | Aruchamy | NaN | None | NaN | NaN |
When you try to execute the following code snippet again, you’ll see the error message that the column already exists.
if 'new_column_3' not in df.columns:
df = df.assign(new_column_3=" ")
else:
print("new_column_1 already exists in the dataframe")
Output
new_column_1 already exists in the dataframe
The column is not added to the dataframe as it already exists.
Add Empty Column at a Specific Position
You can add an empty column at a specific position using the df.insert() method.
- Pass the
index
position to the insert method. - It’ll add the empty column at the specific position and shift the other columns to the right.
- The index is
0
based.
Code
df.insert(0,"Blank_Column_Name", " ")
df
0
– Index position to insert the new columnBlank_Column_Name
– New column header" "
– Value to add to the new column. to add an empty column, you can pass" "
orNp.Nan
orNone
value.
A blank column is added at the index position 0, and other columns will be shifted to the right, as shown below.
Dataframe Will Look Like
Blank_Column_Name | Country | First Name | Last Name | Empty_Column | NaN_Column | None_Column | new_column_1 | new_column_2 | new_column_3 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | India | Vikram | Aruchamy | NaN | None | NaN | NaN | |||
1 | India | Vikram | Aruchamy | NaN | None | NaN | NaN |
Add Empty Columns from a List
To add empty columns from a list, you can get Get the Column Name of the pandas dataframe and use those columns in the list with additional columns.
Snippet
df = df.reindex(columns = df.columns.tolist() + ["new_column_4", "new_column_5"])
df
df.columns.tolist()
– List of the existing columns+
– To concatenate the additional columns to the exisiting columns["new_column_4", "new_column_5"]
– List of new columns.
Dataframe Will Look Like
Blank_Column | Country | First Name | Last Name | Empty_Column | NaN_Column | None_Column | new_column_1 | new_column_2 | new_column_3 | new_column_4 | new_column_5 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | India | Vikram | Aruchamy | NaN | None | NaN | NaN | NaN | NaN | |||
1 | India | Vikram | Aruchamy | NaN | None | NaN | NaN | NaN | NaN |
This is how you can add empty columns from a list using the reindex()
method.