In this tutorial, we will demonstrate how to insert a blank column into a Pandas dataframe in Python. Pandas is a popular library for data manipulation and analysis, and one of its main features is the dataframe object. Dataframes provide an easy way to handle structured data and are essential for data manipulation tasks.

Loading a Sample Dataframe

Please feel free to load the example Pandas dataframe given below if you want to follow the lesson along with it. Although some particular examples may require further customization for your situation, feel free to use your own data if you have it.

Let’s loading some data! 

				
					import pandas as pd

data = {'name': ['Alice', 'Bob', 'Charlie'],
        'age': [25, 30, 35],
        'gender': ['F', 'M', 'M']}

df = pd.DataFrame(data)

print(df)

				
			

Output:

				
					#        name  age gender
# 0     Alice   25      F
# 1       Bob   30      M
# 2  Charlie   35      M

				
			

This will create a dataframe with four columns: “Name”, “Age”, “Gender”.

Insert a Blank Column Using a Pandas Series

By giving a pandas Series object, you can quickly add an empty column to a Pandas dataframe. Recall that Pandas columns are actually Pandas series. We are therefore adding a null column to the dataframe by adding a blank series to the dataframe.

Here’s an example:

				
					import pandas as pd

# create a sample DataFrame
data = {'name': ['Alice', 'Bob', 'Charlie'],
        'age': [25, 30, 35]}

df = pd.DataFrame(data)

# create a blank series with the same length as the DataFrame
blank_series = pd.Series([''] * len(df))

# insert the blank series as a new column at index 1
df.insert(1, 'Blank', blank_series)

# view the updated DataFrame
print(df)

				
			

Output:

				
					#       name   Blank  age
# 0      Alice        25
# 1        Bob        30
# 2    Charlie        35

				
			

In this example, we first created a sample DataFrame with two columns ‘name’ and ‘age’. Then we created a blank series blank_series with the same length as the DataFrame by multiplying an empty string '' with the length of the DataFrame. Finally, we inserted the blank series as a new column with the label ‘Blank’ at index 1 using the insert() method. The resulting DataFrame has a new blank column ‘Blank’ at index 1.

Insert a Blank Column Using np.NaN

Adding a blank column using the numpy NaN (not a number) value is another straightforward method.

A Pandas dataframe column’s single value can be put into all of the column’s values by giving the column a single value. In this instance, np. NaN will be inserted as a missing number into a column.

Here’s an example:

				
					import pandas as pd
import numpy as np

# create a sample DataFrame
data = {'name': ['Alice', 'Bob', 'Charlie'],
        'age': [25, 30, 35]}

df = pd.DataFrame(data)

# insert a blank column with np.NaN at index 1
df.insert(1, 'Blank', np.NaN)

# view the updated DataFrame
print(df)

				
			

Output:

				
					#       name  Blank  age
# 0     Alice    NaN   25
# 1       Bob    NaN   30
# 2   Charlie    NaN   35
 
				
			

In this example, we first created a sample DataFrame with two columns ‘name’ and ‘age’. Then we inserted a blank column with np.NaN as the default value at index 1 using the insert() method. The resulting DataFrame has a new blank column ‘Blank’ at index 1, with np.NaN values in each row.

Insert a Blank Column Using an Empty String

In a manner similar to the case above, if an empty string is inserted into a column, that value will be present in each value there. This method has the advantage that it can be applied without requiring the import of Numpy.

Here’s an example:

				
					import pandas as pd

# create a sample DataFrame
data = {'name': ['Alice', 'Bob', 'Charlie'],
        'age': [25, 30, 35]}

df = pd.DataFrame(data)

# insert a blank column with an empty string at index 1
df.insert(1, 'Blank', '')

# view the updated DataFrame
print(df)

				
			

Output:

				
					#       name Blank  age
# 0     Alice        25
# 1       Bob        30
#  2  Charlie        35

				
			

In this example, we first created a sample DataFrame with two columns ‘name’ and ‘age’. Then we inserted a blank column with an empty string as the default value at index 1 using the insert() method. The resulting DataFrame has a new blank column ‘Blank’ at index 1, with empty string values in each row.

Insert a Blank Column Using Pandas .insert()

A column is added to a Pandas dataframe at a particular location using the.insert() method. Let’s add a new section and another illustration.  Let’s insert a new column!

Here’s an example:

				
					import pandas as pd

# create a sample DataFrame
data = {'name': ['Alice', 'Bob', 'Charlie'],
        'age': [25, 30, 35]}

df = pd.DataFrame(data)

# insert a blank column at index 1 using .insert() and add a third column
df.insert(1, 'Blank', '')
df.insert(3, 'Gender', ['F', 'M', 'M'])

# view the updated DataFrame
print(df)

				
			

Output:

				
					#   name      Blank age  Gender
# 0     Alice        25      F
# 1       Bob        30      M
# 2   Charlie        35      M

				
			

In this example, we first created a sample DataFrame with two columns ‘name’ and ‘age’. Then we inserted a blank column with an empty string as the default value at index 1 using the .insert() method. We also added a third column ‘Gender’ with a list of values at index 3 using the same method. The resulting DataFrame has three columns ‘name’, ‘Blank’, and ‘age’ with a new column ‘Gender’ at index 3.

Wrap up

In this article, you learned several different ways to enter a blank column into a Pandas dataframe, including using an empty string, a pandas Series, assigning np.NaNs, and using the Pandas.insert() method.

To learn more check out: https://pandas.pydata.org/pandas-docs/stable/index.html


Thanks for reading. Happy coding!