How to Add Empty Column to Pandas DataFrame (3 Examples)


You can use the following methods to add empty columns to a pandas DataFrame:

Method 1: Add One Empty Column with Blanks

df['empty_column'] = ""

Method 2: Add One Empty Column with NaN Values

df['empty_column'] = np.nan

Method 3: Add Multiple Empty Columns with NaN Values

df[['empty1', 'empty2', 'empty3']] = np.nan

The following examples show how to use each method with the following pandas DataFrames:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4]})

#view DataFrame
print(df)

  team  points  assists
0    A      18        5
1    B      22        7
2    C      19        7
3    D      14        9
4    E      14       12
5    F      11        9
6    G      20        9
7    H      28        4

Example 1: Add One Empty Column with Blanks

The following code shows how to add one empty column with all blank values:

#add empty column
df['blanks'] = ""

#view updated DataFrame
print(df)

  team  points  assists blanks
0    A      18        5       
1    B      22        7       
2    C      19        7       
3    D      14        9       
4    E      14       12       
5    F      11        9       
6    G      20        9       
7    H      28        4   

The new column called blanks is filled with blank values.

Example 2: Add One Empty Column with NaN Values

The following code shows how to add one empty column with all NaN values:

import numpy as np

#add empty column with NaN values
df['empty'] = np.nan

#view updated DataFrame
print(df)

  team  points  assists  empty
0    A      18        5    NaN
1    B      22        7    NaN
2    C      19        7    NaN
3    D      14        9    NaN
4    E      14       12    NaN
5    F      11        9    NaN
6    G      20        9    NaN
7    H      28        4    NaN

The new column called empty is filled with NaN values.

Example 3: Add Multiple Empty Columns with NaN Values

The following code shows how to add multiple empty columns with all NaN values:

import numpy as np

#add three empty columns with NaN values
df[['empty1', 'empty2', 'empty3']] = np.nan

#view updated DataFrame
print(df)

  team  points  assists  empty1  empty2  empty3
0    A      18        5     NaN     NaN     NaN
1    B      22        7     NaN     NaN     NaN
2    C      19        7     NaN     NaN     NaN
3    D      14        9     NaN     NaN     NaN
4    E      14       12     NaN     NaN     NaN
5    F      11        9     NaN     NaN     NaN
6    G      20        9     NaN     NaN     NaN
7    H      28        4     NaN     NaN     NaN

Notice that all three of the new columns are filled with NaN values.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Rename Columns in Pandas
How to Add a Column to a Pandas DataFrame
How to Change the Order of Columns in Pandas DataFrame

Leave a Reply

Your email address will not be published. Required fields are marked *