moshtaba
moshtaba

Reputation: 381

Doing rather complicated calculations on each group after groupby

I have a dataframe like

import pandas as pd
A = ['x' , 'x', 'y', 'y', 'y']
B = [1, 3, 2, 1, 4]
C = [2, 3, 7, 2, 1]
df = pd.DataFrame({'A' : A, 'B' : B, 'C' : C })
df

and unfortunately I couldn't do this calculation on each group after df.groupby(['A']): multiply B elementwise with C elements and sum them and divide this by sum of elements of B and so heve the following:

df1 = pd.DataFrame({'A' : ['x', 'y'], '(B*C)/B' : [(1*2 + 3*3)/(1+3) , (2*7+1*2+4*1)/(2+1+4)]})
df1

Upvotes: 2

Views: 70

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

You can do an apply:

df.groupby('A').apply(lambda x: (x['B']*x['C']).sum()/x['B'].sum())

Or similarly:

df.groupby('A').apply(lambda x: np.average(x['C'],weights=x['B']) )

Output:

A
x    2.750000
y    2.857143
dtype: float64

Upvotes: 2

Related Questions