Adding a New Empty Column to a pandas DataFrame in Python (Example Code)

In this Python tutorial you’ll learn how to append a new empty column to a pandas DataFrame.

Preparing the Example

import pandas as pd                       # Load pandas library
my_df = pd.DataFrame({'A':range(1, 5),    # Construct new pandas DataFrame
                      'B':['a', 'c', 'a', 'b'],
                      'C':range(15, 11, - 1)})
print(my_df)
#    A  B   C
# 0  1  a  15
# 1  2  c  14
# 2  3  a  13
# 3  4  b  12

Example: Adding a Column with Empty String to a pandas DataFrame

my_df['D'] = ''                           # Append empty string variable
print(my_df)
#    A  B   C D
# 0  1  a  15  
# 1  2  c  14  
# 2  3  a  13  
# 3  4  b  12

Related Articles & Further Resources

Here, you may find some further resources on topics such as naming data and data conversion.

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.
You need to agree with the terms to proceed

Menu
Top