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') 但后来我不知道如何在熊猫中写出我想要的东西。
尝试这个:
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)
| 归档时间: |
|
| 查看次数: |
4937 次 |
| 最近记录: |