groupby obj不同组的Python差异

2024-10-02 00:22:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我将此作为csv在熊猫中工作-前十行:

        yearmonth  MV Quantiles  pead Quantiles  ret_f0f1      rf
0          198301           0.2             0.8 -0.071429  0.0069
1          198301           0.2             0.2  0.010638  0.0069
2          198301           0.2             0.2  0.180328  0.0069
3          198301           0.4             0.2  0.042553  0.0069
4          198301           0.4             0.8  0.000000  0.0069
5          198301           0.2             0.8  0.244889  0.0069
6          198301           0.6             0.8  0.071429  0.0069
7          198301           0.6             1.0 -0.025974  0.0069
8          198301           0.2             0.8  0.097222  0.0069
9          198301           1.0             1.0  0.191489  0.0069

要按['yearmonth'、'MV分位数'、“pead分位数”]进行分组,并创建一列'excess_return',其中包含从列'ret_f0f1'中减去列'rf'的结果:

        yearmonth  MV Quantiles  ...      rf  excess_return
0          198301           0.2  ...  0.0069      -0.071429
1          198301           0.2  ...  0.0069       0.010638
2          198301           0.2  ...  0.0069       0.180328
3          198301           0.2  ...  0.0069       0.042553
4          198301           0.2  ...  0.0069       0.000000
5          198301           0.2  ...  0.0069       0.244889
6          198301           0.2  ...  0.0069       0.071429
7          198301           0.2  ...  0.0069      -0.025974
8          198301           0.2  ...  0.0069       0.097222
9          198301           0.2  ...  0.0069       0.191489

我已经尝试过转换lambda,但不能准确地调用列来执行减法。似乎是因为x是系列而不是df?其他想法是使用x.diff

df2.groupby(['yearmonth','MV Quantiles',"pead Quantiles"])['ret_f0f1','rf'].transform(lambda x: x['ret_f0f1']-x['rf'])

现在,我正在使用:

df2[df2['pead Quantiles']==1.0].groupby(['yearmonth','MV Quantiles','pead Quantiles'])['ret_f0f1'].mean().reset_index()['ret_f0f1']-df2[df2['pead Quantiles']==0.2].groupby(['yearmonth','MV Quantiles','pead Quantiles'])['ret_f0f1'].mean().reset_index()['ret_f0f1']


Tags: lambdareturnmeanresetdf2groupbyretrf

热门问题