It is possible to return any number of aggregated values from a groupby object with apply
. Simply, return a Series and the index values will become the new column names.
Let's see a quick example:
df = pd.DataFrame({'group':['a','a','b','b'],
'd1':[5,10,100,30],
'd2':[7,1,3,20],
'weights':[.2,.8, .4, .6]},
columns=['group', 'd1', 'd2', 'weights'])
df
group d1 d2 weights
0 a 5 7 0.2
1 a 10 1 0.8
2 b 100 3 0.4
3 b 30 20 0.6
Define a custom function that will be passed to apply
. It implicitly accepts a DataFrame - meaning the data
parameter is a DataFrame. Notice how it uses multiple columns, which is not possible with the agg
groupby method:
def weighted_average(data):
d = {}
d['d1_wa'] = np.average(data['d1'], weights=data['weights'])
d['d2_wa'] = np.average(data['d2'], weights=data['weights'])
return pd.Series(d)
Call the groupby apply
method with our custom function:
df.groupby('group').apply(weighted_average)
d1_wa d2_wa
group
a 9.0 2.2
b 58.0 13.2
You can get better performance by precalculating the weighted totals into new DataFrame columns as explained in other answers and avoid using apply
altogether.