Pandas groupby方法中的group_keys属性

pandas版本1.5.3中groupby方法,当设置group_keys=True时,会以groupby的字段为第一级索引,如下述代码中time_id作为第一级索引,同时保留了原dataframe(df)中的索引作为第二级索引。

>>> df.groupby(['time_id'], group_keys=True)['wap'].apply(log_return)
time_id         
0        0               NaN
         1          0.000000
         2          0.000000
         3          0.000000
         4          0.000000
                      ...   
26454    5237975   -0.001228
         5237976    0.000491
         5237977   -0.005031
         5237978    0.003219
         5237979    0.003264
Name: wap, Length: 5237980, dtype: float64

group_keys的意思就是是否保留groupby的feature(如time_id)作为keys放入结果中,True是放,False是不放。这也印证了帮助里的说明:
group_keys : bool, optional
When calling apply and the by argument produces a like-indexed
(i.e. :ref:a transform <groupby.transform>) result, add group keys to
index to identify pieces
. By default group keys are not included
when the result’s index (and column) labels match the inputs, and
are included otherwise. This argument has no effect if the result produced
is not like-indexed with respect to the input.
因此,当设置group_keys=False时,group keys(time_id)就不在返回结果中了,如下所示。在设置为False是可以直接将返回结果,作为原dataframe(df)的一列,很方便。

>>> df.groupby(['time_id'], group_keys=False)['wap'].apply(log_return)
0               NaN
1          0.000000
2          0.000000
3          0.000000
4          0.000000
             ...   
5237975   -0.001228
5237976    0.000491
5237977   -0.005031
5237978    0.003219
5237979    0.003264
Name: wap, Length: 5237980, dtype: float64

PS:对英文帮助的深入理解,需要结合实际应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值