How to add Empty Column to Dataframe in Pandas?
In Pandas we add empty columns to a DataFrame to create placeholders for future data or handle missing values. We can assign empty columns using different methods depending on the type of placeholder value we want. In this article, we will see different methods to add empty columns and how each one works.
Lets see an example where we will add an empty column with an empty string (' '). We will be using Numpy and Pandas libraries for its implementation.
import pandas as pd
Mydataframe = pd.DataFrame({'FirstName': ['Ansh', 'Ashish', 'Milan'],
'Age': [21, 22, 23]})
print("---Original DataFrame---\n", Mydataframe)
Mydataframe['Gender'] = ''
Mydataframe['Department'] = ''
print("---Updated DataFrame with Empty Strings---\n", Mydataframe)
Output:

Syntax:
DataFrame['NewColumn'] = value
Where value can be:
- ' ' for an empty string
- None for null values
- np.nan for missing numerical values
Lets see more examples of this:
Example 1: Adding an Empty Column with NaN
When dealing with numerical data or missing values NaN values is a commonly used. We need to import NumPy to use np.nan.
import numpy as np
Mydataframe['Gender'] = ''
Mydataframe['Department'] = np.nan
print("---Updated DataFrame with NaN---\n", Mydataframe)
Output:

Example 2: Adding an Empty Column with None
None is useful when we want a placeholder that represents a "null" or missing data.
Mydataframe['Gender'] = None
Mydataframe['Department'] = None
print("---Updated DataFrame with None---\n", Mydataframe)
Output:

Example 3: Adding Empty Columns Using Dataframe.reindex()
We can use the reindex() method to add new columns with NaN values by default. For example we have created a Pandas DataFrame with two columns "FirstName" and "Age". We will apply Dataframe.reindex() method to add two new columns "Gender" and " Roll Number" to the list of columns with NaN values.
import pandas as pd
Mydataframe = pd.DataFrame({'FirstName': ['Preetika', 'Tanya', 'Akshita'],
'Age': [25, 21, 22]})
print("---Original DataFrame---\n", Mydataframe)
Mydataframe = Mydataframe.reindex(columns=Mydataframe.columns.tolist() + ['Gender', 'Roll Number'])
print("---Updated DataFrame with reindex()---\n", Mydataframe)
Output:

Example 4: Adding Empty Columns Using insert()
The insert() method adds a new column at a specified position in the DataFrame. In this example we will add an empty column of "Roll Number" using Dataframe.insert().
Mydataframe = pd.DataFrame({'FirstName': ['Rohan', 'Martin', 'Mary'],
'Age': [28, 39, 21]})
print("---Original DataFrame---\n", Mydataframe)
Mydataframe.insert(0, 'Roll Number', '')
print("---Updated DataFrame with insert()---\n", Mydataframe)
Output:

With these simple methods we can easily add empty columns to our DataFrame for placeholders for future data or handling missing values as needed.