Adding calculated column in Pandas

Learn, how to add a calculated column in pandas DataFrame?
Submitted by Pranit Sharma, on November 05, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Columns are the different fields that contain their particular values when we create a DataFrame. We can perform certain operations on both row & column values.

Suppose, we are given a DataFrame of some employees which contains different columns like name, age, salary, and department. We need to add some columns pf, bonus, and hra. We know these values are some percentage of the salary of employees.

Let us assume every employee gets a bonus pf 15% of his salary, now we need to calculate 15% of the salary of each employee and store it in the corresponding bonus column of that employee.
For this purpose, we can either define different functions for adding all three new columns or we can directly calculate these values.

Let us understand with the help of an example,

Python code to add a calculated column in pandas DataFrame

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a DataFrame
df = pd.DataFrame({
    'name':['shan','sonu','tina','raj'],
    'age':[20,21,23,20],
    'salary':[200000,210000,230000,200000]
})

# Display dataframe
print('Original DataFrame:\n',df,'\n')

# adding new columns
df['pf'] = df['salary']*0.12
df['bonus'] = df['salary']*0.15

# Display result
print('Result:\n',df,'\n')

Output:

Example: Adding calculated column in Pandas

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




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

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