Home »
Python »
Python Programs
Calculate new column as the mean of other columns in pandas
Given a pandas dataframe, we have to calculate new column as the mean of other columns.
By Pranit Sharma Last updated : October 02, 2023
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.
Calculating new column as the mean of other columns
The most fascinating key point about pandas is that it contains numerous amounts of function to calculate almost everything mathematically and logically.
With the help of pandas, we can calculate the mean of any column in a DataFrame, the column values should be integer or float values and not string.
pandas.DataFrame.mean()
Mean is nothing but an average value of a series of a number. Mathematically, the mean can be calculated as:
Here, x̄ is the mean, ∑x is the summation of all the values and n is the total number of values/elements.
Suppose we have a series of numbers from 1 to 10, then the average of this series will be:
∑x = 1+2+3+4+5+6+7+8+9+10
∑x = 55
n = 10
x̄ = 55/10
x̄ = 5.5
But in pandas, we use pandas.DataFrame['col'].mean() directly to calculate the average value of a column.
Now we will create a new column and calculate the average along the row.
Let us understand with the help of an example,
Python program to calculate new column as the mean of other columns in pandas
# Importing pandas package
import pandas as pd
# Creating two dictionaries
d = {
'A':[10,19,29,45,33],
'B':[90,78,56,21,13],
'C':[10,19,59,70,60]
}
# Creating DataFrame
df = pd.DataFrame(d)
# Display Original DataFrame
print("Created DataFrame:\n",df,"\n")
# calculating mean along the row
df['Mean'] = df.mean(axis=1)
# Display result
print("Modified DataFrame:\n",df)
Output
The output of the above program is:
Python Pandas Programs »