Home »
Python »
Python Programs
How to add an empty column to a DataFrame?
Given a DataFrame, we have to add an empty column in it.
Submitted by Pranit Sharma, on April 27, 2022
Columns are the different fields that contain their particular values when we create a DataFrame. We can perform certain operations on both rows & column values. Adding an empty column to the DataFrame is possible and easy as well. Let us understand, how we can add an empty DataFrame to the DataFrame?
To add an empty column in the existing DataFrame, we need to pass a new column name as an index to the DataFrame,
Syntax:
df['col_name'] = "col_value"
To work with Python Pandas, we need to import the pandas library. Below is the syntax,
import pandas as pd
Let us understand with the help of an example.
# Importing pandas package
import pandas as pd
# Creating a Dictionary
dict = {'Name':['Amit','Bhairav','Chirag','Divyansh','Esha'],
'DOB':['07/12/2001','08/11/2002','09/10/2003','10/09/2004','11/08/2005'],
'Gender':['Male','Male','Male','Male','Female']}
# Creating a DataFrame
df = pd.DataFrame(dict)
# Display original DataFrame
print("Original DataFrame:\n",df,"\n")
# Adding a new column named Empty and
# passing an empty string as its value
df['Empty'] = " "
# Display modified DataFrame
print("Modified DataFrame:\n",df)
Output:
Python Pandas Programs »
ADVERTISEMENT
ADVERTISEMENT