Oleksandra Oleksandra - 4 years ago 143
Python Question

How to calculate a value inside a group after groupby pandas

Suppose I have a pandas DataFrame:

a b c d .... z
1 10 3 .
1 20 4 .
2 30 5 .
3 40 6 .
3 50 7 . .... .


I want to produce a DataFrame:

a *not sure how to refer to this column?*
1 (10+20)/(3+4)
2 30/5
3 (40+50)/(6+7)


How do I do that? Also, how to refer to the created column?

I have tried df.groupby('a') but then I don't know how to write what I want in pandas.

Answer Source

try this:

In [216]: df.groupby('a').apply(lambda x: x['b'].sum()/x['c'].sum())
Out[216]:
a
1    4.285714
2    6.000000
3    6.923077
dtype: float64
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download