Uylenburgh
Uylenburgh

Reputation: 1307

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.

Upvotes: 7

Views: 12340

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

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

Upvotes: 12

Related Questions