Skip to content

Insert a Blank Column to Pandas Dataframe

Insert a Blank Column Pandas Dataframe

In this post, you’ll learn how to add insert a blank column to a Pandas dataframe.

This is quite easy to do and this post will explore a number of different examples that you can use to insert an empty column to a Pandas dataframe.

Loading our Dataframe

Let’s begin by creating a dataframe that we can use throughout the tutorial. We’ll use the Pandas .from_dict() method in order to load in our dataframe. After that, we’ll print out the dataframe:

import pandas as pd

df = pd.DataFrame.from_dict({
    'Column 1': [1,2,3,4,5],
    'Column 2': ['a','b', 'c', 'd', 'e']
})

print(df)

# Returns
#    Column 1 Column 2
# 0         1        a
# 1         2        b
# 2         3        c
# 3         4        d
# 4         5        e

Let’s get started by looking at some examples.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Insert a Blank Column Using a Pandas Series

One easy way to insert an empty column into a Pandas dataframe is by assigning a pandas Series object. Keep in mind, Pandas columns are really a Pandas series. So, by inserting a blank series into the dataframe we’re inserting a blank column into the dataframe:

df = pd.DataFrame.from_dict({
    'Column 1': [1,2,3,4,5],
    'Column 2': ['a','b', 'c', 'd', 'e']
})

df['Column 3'] = pd.Series()

print(df)

# This returns:
#    Column 1 Column 2  Column 3
# 0         1        a       NaN
# 1         2        b       NaN
# 2         3        c       NaN
# 3         4        d       NaN
# 4         5        e       NaN

Insert a Blank Column Using np.NaN

Another simple way to insert a blank column into a Pandas dataframe is by using the numpy NaN (not a number) value.

By assigning a single value to a Pandas dataframe column, that value is inserted into each value for that column. In this case, we’ll be inserting a missing value, specifically the np.NaN, into a column:

import pandas as pd
import numpy as np

df = pd.DataFrame.from_dict({
    'Column 1': [1,2,3,4,5],
    'Column 2': ['a','b', 'c', 'd', 'e']
})

df['Column 3'] = np.NaN

print(df)

# This returns:
#    Column 1 Column 2  Column 3
# 0         1        a       NaN
# 1         2        b       NaN
# 2         3        c       NaN
# 3         4        d       NaN
# 4         5        e       NaN

Insert a Blank Column Using an Empty String

Similar to the example above, by inserting an empty string into a column, that value will be carried throughout each value in that column.

The benefit of this approach is that you don’t need to import numpy in order to use this solution:

import pandas as pd

df = pd.DataFrame.from_dict({
    'Column 1': [1,2,3,4,5],
    'Column 2': ['a','b', 'c', 'd', 'e']
})

df['Column 3'] = ""

print(df)

# This returns:
#    Column 1 Column 2 Column 3
# 0         1        a         
# 1         2        b         
# 2         3        c         
# 3         4        d         
# 4         5        e         

Insert a Blank Column Using Pandas .insert()

The Pandas .insert() method inserts a column into a Pandas dataframe at a specific location. You can learn more about the method by checking out the official documentation. Let’s insert a new column with another example:

import pandas as pd

df = pd.DataFrame.from_dict({
    'Column 1': [1,2,3,4,5],
    'Column 2': ['a','b', 'c', 'd', 'e']
})

df['Column 3'] = df.insert(2, 'Column 3', '')

print(df)

# This returns:
#    Column 1 Column 2 Column 3
# 0         1        a     None
# 1         2        b     None
# 2         3        c     None
# 3         4        d     None
# 4         5        e     None

Conclusion

In this post, you learned how to a blank column into a Pandas dataframe using multiple different methods, including using an empty string, a pandas Series, assigning np.NaNs, and using the Pandas .insert() method.

Additional Resources

To learn more about related topics, check out the resources below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

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