HomeLinuxpandas Add Column With Fixed Worth

pandas Add Column With Fixed Worth


Python’s Pandas library supplies a lot of modules and features for analyzing information and manipulation. Whereas working with Pandas, generally we have to add/insert a specified column with a relentless worth to an current DataFrame. A relentless worth is a price that doesn’t change or differ throughout the rows of the DataFrame. Including a column with a relentless worth will be helpful for labeling, filtering, grouping, or merging your information.

This Python weblog will clarify an in depth information on including columns with a relentless worth to a selected DataFrame.

The way to Add/Insert Columns With Fixed Worth in Pandas?

The beneath approaches are used so as to add columns with fixed values in Pandas DataFrame:

Technique 1: Add Column With Fixed Worth in Pandas DataFrame Utilizing the “Sq. Bracket”

The sq. bracket notation provides columns with fixed values in Pandas DataFrame. The next instance is used so as to add a column with a relentless worth in Pandas DataFrame:

import pandas
data1 = ({‘Identify’:[“Anna”,“Jon”,“Cyndy”], ‘roll_no’ :[12, 15, 17],‘marks’:[50, 82, 94]})
data_frame1 = pandas.DataFrame(data1)
print(‘Given DataFrame:n, data_frame1)
data_frame1[“Total”] = 100
print(n, data_frame1)

Within the above code, first, the “pandas” library is imported, and the “pandas.DataFrame()” operate is utilized to create/assemble a DataFrame. After that, the “data_frame1[“Total”] = 100” syntax is used so as to add the columns with the fixed worth “100” to the required information body.

Output

The above snippet exhibits that the column with a relentless worth has been added.

Technique 2: Add Column With Fixed Worth in Pandas DataFrame Using the “df.insert()”

The “dataframe.insert()” methodology inserts the DataFrame on the specified location. This methodology provides the column with distinction worth in Pandas DataFrame. The working of the “df.insert()” methodology for including a column with a relentless worth to Pandas is demonstrated within the following code snippet:

import pandas
data1 = ({‘Identify’:[“Anna”,“Jon”,“Cyndy”], ‘roll_no’ :[12, 15, 17], ‘obtained_marks’:[50, 82, 94]})
data_frame1 = pandas.DataFrame(data1)
print(‘Given DataFrame:n, data_frame1)
data_frame1.insert(2, ‘Total_marks’, ‘100’)
print(n, data_frame1)

Right here, the “df.insert()” takes the required index worth “2”, column identify “Total_marks”, and the fixed worth “100” as arguments and provides the column with a relentless worth to the Pandas DataFrame.

Output

The column with fixed worth has been added to the precise location of the Pandas DataFrame.

Technique 3: Add Column With Fixed Worth in Pandas DataFrame Using the “df.assign()”

The “df.assign()” methodology can be used so as to add/insert a column with a relentless worth to the DataFrame. Let’s overview the next instance:

import pandas
data1 = ({‘Identify’:[“Anna”,“Jon”,“Cyndy”], ‘roll_no’ :[12, 15, 17], ‘obtained_marks’:[50, 82, 94]})
data_frame1 = pandas.DataFrame(data1)
print(‘Given DataFrame:n, data_frame1)
df = data_frame1.assign(Total_marks=100)
print(n, df)

Within the above code, the “df.assign()” takes the column identify with fixed worth as an argument and retrieves the DataFrame by including a column.

Output

Within the above snippet, the Pandas DataFrame has been up to date with the column worth with a relentless worth.

Technique 4: Add Column With Fixed Worth in DataFrame Using the “df.apply()”

In Python, the “apply()” methodology is utilized to use a specified operate alongside one of many DataFrame axis or index row axis. The beneath code is used so as to add a column with a relentless worth in Pandas DataFrame:

import pandas
data1 = ({‘Identify’:[“Anna”,“Jon”,“Cyndy”], ‘roll_no’ :[12, 15, 17],‘obtained_marks’:[50, 82, 94]})
data_frame1 = pandas.DataFrame(data1)
print(‘Given DataFrame:n, data_frame1)
data_frame1[‘Total_marks’] = data_frame1.apply(lambda x: 100, axis = 1)
print(n, data_frame1)

Right here, the “df.apply()” methodology takes the “lambda” operate as an argument and provides a specified column with a relentless worth to the DataFrame.

Output

The desired column has been added to the given DataFrame.

Conclusion

The “Sq. Bracket”, “df.insert()”, “df.assign()”, and the “df.apply()” strategies are utilized to insert/add a column with a relentless/mounted worth to DataFrame. These strategies can add particular columns with fixed values to the specified location within the Pandas DataFrame. This weblog has offered an in depth information on including a column with a relentless worth to the required location.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments