如何在groupby熊猫之后计算组内的值

Sas*_*kia 4 python group-by pandas

假设我有一个 Pandas DataFrame:

a   b   c  d  .... z
1   10  3  .
1   20  4  .
2   30  5  .
3   40  6  .
3   50  7  .  ....  .
Run Code Online (Sandbox Code Playgroud)

我想生成一个数据帧:

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

我怎么做?另外,如何引用创建的列?

我试过 df.groupby('a') 但后来我不知道如何在熊猫中写出我想要的东西。

Max*_*axU 6

尝试这个:

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
Run Code Online (Sandbox Code Playgroud)