Q:

How to add an empty column to a DataFrame?

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Pandas DataFrame - Get first row value of a given ... >>
<< Create Pandas DataFrame from a string...