How to Add an Empty Column to Pandas Dataframe – Detailed Guide

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.

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.

Snippet

df["Empty_Column"] = " "

df

An empty column will be added at the end of the dataframe with the column header Empty_Column.

Dataframe Will Look Like

CountryFirst NameLast NameEmpty_Column
0IndiaVikramAruchamy
1IndiaVikramAruchamy

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.

Once added, you can select rows from pandas dataframe based on condition (having empty values) to check if the empty column is added appropriately.

Using NP.nan

You can add a column with np.nan to create a blank column with nan values, as shown below.

Snippet

import numpy as np

df["NaN_Column"] = np.nan

df

Dataframe Will Look Like

CountryFirst NameLast NameEmpty_ColumnNaN_Column
0IndiaVikramAruchamyNaN
1IndiaVikramAruchamyNaN

Using None

You can also use None to create empty or blank columns in the dataframe, as shown below.

df["None_Column"] = None

df

Dataframe Will Look Like

CountryFirst NameLast NameEmpty_ColumnNaN_ColumnNone_Column
0IndiaVikramAruchamyNaNNone
1IndiaVikramAruchamyNaNNone

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.

You can use the reindex() method to add multiple columns.

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, First, get the list of existing columns in the dataframe by using df.columns.tolist() and add the additional columns to the list.

The newly added columns will have NaN values by default to denote the missing values.

Then, you can 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

CountryFirst NameLast NameEmpty_ColumnNaN_ColumnNone_Columnnew_column_1new_column_2
0IndiaVikramAruchamyNaNNoneNaNNaN
1IndiaVikramAruchamyNaNNoneNaNNaN

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 already exist in the dataframe.

To do this, 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.

Use the below Snippet to add an empty column named new_column_3 if it doesn’t exist already in the dataframe.

Snippet

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

CountryFirst NameLast NameEmpty_ColumnNaN_ColumnNone_Columnnew_column_1new_column_2new_column_3
0IndiaVikramAruchamyNaNNoneNaNNaN
1IndiaVikramAruchamyNaNNoneNaNNaN

When you try to execute the below code snippet again, then 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.

You can 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.

Snippet

df.insert(0,"Blank_Column_Name", " ")

df
  • 0 – Index position to insert the new column
  • Blank_Column_Name – New column header
  • " " – Value to add to the new column. to add an empty column, you can pass " " or Np.Nan or None value.

A blank column will be added at the index position 0, and other columns will be shifted to the right, as shown below.

Dataframe Will Look Like

Blank_Column_NameCountryFirst NameLast NameEmpty_ColumnNaN_ColumnNone_Columnnew_column_1new_column_2new_column_3
0IndiaVikramAruchamyNaNNoneNaNNaN
1IndiaVikramAruchamyNaNNoneNaNNaN

Add Empty Columns from a List

To add empty columns from a list, you can check Get the Column Name of 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_ColumnCountryFirst NameLast NameEmpty_ColumnNaN_ColumnNone_Columnnew_column_1new_column_2new_column_3new_column_4new_column_5
0IndiaVikramAruchamyNaNNoneNaNNaNNaNNaN
1IndiaVikramAruchamyNaNNoneNaNNaNNaNNaN

This is how you can add empty columns from a list using the reindex() method.

Conclusion

To summarize, you’ve learned how to add empty columns to pandas dataframe. These empty columns are used as a placeholder to denote the missing values to which the values can be added later.

If you have any questions, comment below.

You May Also Like

Leave a Comment