ADVERTISEMENT
ADVERTISEMENT

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:

Output | Add an empty column to a DataFrame

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.