Add Empty Column to pandas DataFrame in Python (2 Examples)

 

In this tutorial, I’ll show how to add a new empty column to a pandas DataFrame in Python.

The tutorial will contain these content blocks:

So now the part you have been waiting for – the examples!

 

Example Data & Libraries

First, we need to load the pandas library:

import pandas as pd                       # Import pandas

We’ll use the following data as a basis for this Python programming tutorial:

data = pd.DataFrame({'x1':range(0, 7),    # Create pandas DataFrame
                     'x2':['x', 'y', 'y', 'x', 'y', 'y', 'x'],
                     'x3':range(1, 8)})
print(data)                               # Print pandas DataFrame

 

table 1 DataFrame add empty column pandas dataframe python

 

Have a look at the previous table. It shows that our example pandas DataFrame contains seven rows and three columns.

 

Example 1: Add Empty String Column to pandas DataFrame

In Example 1, I’ll demonstrate how to append a new variable containing an empty character string to an already existing pandas DataFrame in Python.

For this task, we can use the Python syntax below:

data_new1 = data.copy()                   # Duplicate pandas DataFrame
data_new1['new_col'] = ''                 # New column with empty string
print(data_new1)                          # Print new pandas DataFrame

 

table 2 DataFrame add empty column pandas dataframe python

 

After running the previous Python code the new pandas DataFrame shown in Table 2 has been created. As you can see, we have added a completely empty column called new_col to the right side of our input data set.

 

Example 2: Add NaN Column to pandas DataFrame

The following Python syntax shows how to join a new column containing NaN values to a pandas DataFrame.

To accomplish this, we can use the float function as shown below:

data_new2 = data.copy()                   # Duplicate pandas DataFrame
data_new2['new_col'] = float('NaN')       # New column with NaN
print(data_new2)                          # Print new pandas DataFrame

 

table 3 DataFrame add empty column pandas dataframe python

 

By running the previously shown Python syntax, we have created Table 3, i.e. another pandas DataFrame where we have merged an NaN column to our input DataFrame.

 

Video & Further Resources

Do you need further info on the Python programming codes of this article? Then I recommend watching the following video on my YouTube channel. In the video, I’m explaining the Python programming syntax of this article in a live session.

 

 

Additionally, you might want to have a look at the related tutorials on https://statisticsglobe.com/. Some articles can be found below.

 

In summary: In this article you have learned how to combine and append a new empty column to a pandas DataFrame in Python programming. If you have additional questions and/or comments, please let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top