Reputation: 1307
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
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