How to add a column to DataFrame with constant value?

Given a DataFrame, we have to add a column to DataFrame with constant value.
Submitted by Pranit Sharma, on May 12, 2022

Columns are the different fields which contains their particular values when we create a DataFrame. We can perform certain operations on both rows & column values. Sometimes we need to add a column in our dataset and this creation depends upon some condition. Here, we are creating a new column in the DataFrame where all the values of the column are same i.e., constant.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example:

# Importing Pandas package as pd
import pandas as pd

# Creating a dictionary
d = {

# Creating DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Creating a new column with similar/ constant value
df['C'] = pd.Series(['Hello, This is a new column' for i in range(len(df.index))])

# Display modified DataFrame
print("Modified DataFrame\n",df)


Example: Add a column to DataFrame with constant value

Python Pandas Programs »



Comments and Discussions!





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

© some rights reserved.