Using groupby and agg, is it possible to, in the same expression, get the ‘count’ divided by ‘sum’ returned?
JavaScript
x
5
1
data=pd.DataFrame({'id':[1,1,1,2,2,3,3,3,3,4, 4],
2
'val':[10,11,10,13,12,15,12,9, 8, 14, 2]})
3
4
data.groupby(['id'])['val'].agg(['count','sum'])
5
Advertisement
Answer
Assuming this is a dummy example (else just compute the mean
), yes it is possible to combine aggregators using apply
:
JavaScript
1
2
1
out = data.groupby(['id'])['val'].apply(lambda g: g.count()/g.sum())
2
Better alternative in my opinion:
JavaScript
1
6
1
g = data.groupby(['id'])['val']
2
out = g.count()/g.sum()
3
4
# as a one-liner in python ≥3.8
5
# out = (g:=data.groupby(['id'])['val']).count().div(g.sum())
6
output:
JavaScript
1
7
1
id
2
1 0.096774
3
2 0.080000
4
3 0.090909
5
4 0.125000
6
Name: val, dtype: float64
7