To calculate average in pandas data frame of column B with respect to different values of rows in A column pandas groupby function is used.

Calculate average using pandas:

to begin with we have to read data from mean.xlsx file which contains

dataframe
1.Data Frame
import pandas as pd
data=pd.read_excel('mean.xlsx')

then we have to use groupby function to group data by A column and calculate mean(average)

d1=d.groupby(['A']).mean()

pandas merge will merge data frame d with d1 and inner join.

n=pd.merge(d,d1,on='A',how='inner')

below code will rename column name

n.rename(columns={'B_y':'Average of B'},inplace=True)
calculate average using pandas
2.Average in Last Column w.r.t values of column A.Output

Above is how we can calculate average using pandas python.

By SC

Leave a Reply

Your email address will not be published. Required fields are marked *