How to Add Empty Column in DataFrame in Python

Recently, while working on the python pandas project I came across a scenario where I have to add an empty column in the dataframe. This empty column will be filled with the values sooner or later in the project.

So in this Python Pandas tutorial, I have explained different ways to add empty columns in the dataframe in Python. Also, I have covered these points: –

  • Add empty column in dataframe python
  • Add a new column to dataframe pandas at a specific position
  • Pandas create empty columns with name
  • Add multiple empty columns to the dataframe pandas
  • Pandas add empty columns from the list

Add Empty Column in DataFrame Python

A dataframe is a collection of data in rows and columns format. In python, a dataframe can be created using the Pandas library. Empty columns in pandas are called missing values and they are referred to with NaN (Not a Number).

Zips Car Wash is a popular car cleaning company in the United States, they have been maintaining customer details for a long time.

Suppose, recently they have decided to add a new column – gender in their database so that later they can find out which gender arrives often to get the car cleaned.

Now since they are adding a new column so this new column will remain empty for a while and as the customer will visit again they will try to replace the nan with values.

Let’s understand the simplest way to add an empty column in a dataframe python. Read the complete blog to understand other ways of adding empty columns in dataframe pandas.

I am using a dummy carwash dataset to demonstrate how to add an empty column in dataframe pandas. Click on the below button to download the dataset.

This is what the current dataset looks like, now I am going to add a new empty column – Gender to this dataset.

Add an empty column in the dataframe pandas, Add Empty Column in DataFrame Python, Pandas Create Empty Column with Name
Add an empty column in the dataframe pandas

Add an empty column to pandas dataframe using a quote

Quote (‘ ‘) is used to create a string data type in python. Anything written inside a quote will be treated as a string in python and the same thing happens with pandas as well.

In the below code, carwash_df is the dataframe shown above, and when using the assignment (=) operator I have passed an empty quote(‘ ‘) then, the new empty column is created.

carwash_df['Gender'] = ' '

Alternatively, the same results can be obtained when used with double quotes in the python pandas dataframe.

carwash_df['Gender'] = " "

Please Note: – Since pandas do not treat empty strings the same as missing values that is why when I checked for missing values in the dataset, pandas threw False for the Gender column.

Also, it is not displaying nan in the gender column as shown in the image below.

Add Empty Column in DataFrame Python, Pandas Create Empty Column with Name
Add Empty Column in DataFrame Python

Add an empty column to pandas dataframe using np.nan

NaN refers to the missing values and there is no concrete information about its full name. Most developers call it not a number (NaN).

If I will add a column to the pandas dataframe with nan values, this will signify that the column is empty or the column has no values.

carwash_df['Gender'] = np.nan

Make sure to import NumPy before executing the above code. In case you are getting an Import Error – No module named numpy then install NumPy either through Pip or Conda. Learn More Import Error – No module named numpy

In the below output of the above code snippet, the Gender column has missing values (NaN).

Add Empty Column in DataFrame Python pandas
Add Empty Column in DataFrame Python pandas

Add an empty column to the pandas dataframe using None

None as the word suggest refers to nothing and is used as a placeholder. Creating a column with value=None signifies that the column is empty in python pandas. And pandas treat None as a missing value.

Add an empty column to pandas dataframe using None
Add an empty column to the pandas dataframe using None

In this section, I have explained three ways to add empty columns in dataframe pandas python. The same approach can be followed for pandas to create other empty columns with names. Where you can display the full name of the customer in python pandas.

Read Python Pandas Drop Rows Example

Add New Column to DataFrame Pandas at Specific Position

Previously, I added a new column Gender in the carwash dataframe. This new column was added at the last. So now, the company wants to shift the column to a specific position in python pandas.

The objective is to shift the Gender column at index 2. There are three ways to add a new column to dataframe pandas at specific positions: –

  • Delete the existing column and then add a new column to dataframe pandas at specific positions using the Insert() method in python pandas.
  • Change the position of the existing column using reindex() method in python pandas.

I will show you both the ways to add columns at a specific position in python pandas. Read the complete blog.

Add a column to the dataframe at a specific position using the Insert() method

Before I insert the new column in the dataframe make sure the duplicate column is already deleted to avoid confusion. Since, Since, I am resuming the above example so here is the code to remove it.

# remove Gender Column
carwash_df_test = carwash_df.drop(columns=['Gender'])

Note: – Carwash_df_test is the new dataframe created that stores the copy of carwash_df.

Now let’s add a new column to dataframe pandas at a specific position using the insert() method in python pandas.

carwash_df_test.insert(
    loc=2, 
    column='Gender', 
    value=None
)

Parameters description:

  • loc: index number where you want to add a new column
  • Column: Name of the column
  • Value: value provided here will be applied to all the rows of the column. None will display None.

The output in the below image shows new column Gender has been added at the index=2 in python pandas.

Pandas insert an empty column at a specific position
Pandas insert an empty column at a specific position

This method is ideal for the situation when you want to add a new column to dataframe pandas at a specific position.

Add a column to the dataframe at a specific position using reindex() method

The reindex() method in python pandas allows the repositioning or shifting of the existing column from one position to another position.

We are using carwash_df here which already has the Gender column present at the end as shown in the below image.

Add Empty Column in DataFrame Python pandas, Pandas Insert Empty Column at Specific Position
Add a column to the dataframe at a specific position using reindex() method

Code snippet:

Here is the code to implement reindex to change the position of the Gender column in python pandas.

carwash_df = carwash_df.reindex(columns=['First_Name', 'Last_Name', 'Gender', 'Phone', 'Email', 'Branch ', 'Total Washes'])

Please pay attention to the Gender column, we have put it at the specific position in python pandas.

Parameter Description:

  • columns: Provide the list of columns with the new arrangement of column names.

The below image shows the output of the above code, here Gender column has been shifted from index=6 to index=2 in python pandas.

Pandas insert an empty column at a specific position
Pandas insert an empty column at a specific position

This method is ideal for the situation when you want to add an existing column to dataframe pandas at a specific position.

With this, we have come to the end of this section where we have learned pandas insert an empty column at specific positions.

Read How to Drop Duplicates using drop_duplicates() function in Python Pandas

Add Multiple Empty Columns to DataFrame Pandas

Zips Car Wash company has multiple branches in the states of the USA. Also, they have decided to expand in a few European countries like the United Kingdom, Switzerland, Italy, Spain, and Germany.

So now they want to add complete branch addresses in multiple columns to dataframe pandas. These are the new columns they want to add to the pandas dataframe:

  • Street
  • City/Town
  • State/Province/Region
  • Zip/Postal Code
  • Latitude
  • Longitude

This is a handful of columns that has to be added in the carwash_df. There are two ways to add multiple empty columns to dataframe pandas:-

  1. Add multiple empty columns
  2. Pandas add empty columns from the list

Let us go ahead and explore both ways and understand how to add multiple columns in python pandas.

Add multiple empty columns

Multiple empty columns can be created in python pandas by passing the names of all the columns in the 2 lists as shown in the below code snippet.

All the empty columns will have None as a value, you can also use np.nan, quotes (‘ ‘),(” “) in the place of None.

carwash_df[['Street', 'City', 'State', 'Zip code', 'Latitude', 'Longitude']] = None
Add multiple empty columns in python pandas
Add multiple empty columns in python pandas

Pandas Add Empty Columns from the List

A list of empty columns can be looped through to add to the pandas dataframe. In python, we have for & while loops using which can add empty columns from the list.

In the below code snippet, we have demonstrated both for and while loops to add empty columns from the list. And the default value assigned to each column is None. You can change them from None to missing values (np.nan) or an empty string (‘ ‘).

# list of new columns
new_col = ['Street', 'City', 'State', 'Zip code', 'Latitude', 'Longitude']

# add empty columns using for loop
for i in new_col:
    carwash_df_test[i] = None

# drop the columns
carwash_df_test.drop(columns=new_col)

# add empty columns using while loop 
while len(new_col) < 0:
    test = carwash_df_test[i] = None
image
Pandas Add Empty Columns from the List

This is not it, you can also use built-in operations – assign(), apply(), insert(), reindex() to add multiple empty columns to dataframe pandas in python.

Conclusion

In this tutorial, we have learned various ways to add empty columns in dataframe python pandas. We have explored adding a new column in the dataframe using empty strings, None, and missing values (nan). Also, we have covered these topics:

  • Add empty column in dataframe python
  • Add a new column to dataframe pandas at a specific position
  • Pandas create empty columns with name
  • Add multiple empty columns to the dataframe pandas
  • Pandas add empty columns from the list

You may like the following Python Pandas tutorials: