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.

Problem statement

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.

Adding calculated column in Pandas

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 program 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({

# 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


The output of the above program is:

Example: Adding calculated column in Pandas

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.