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
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)
Above is how we can calculate average using pandas python.