文章目录
- 当设置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
- 是否保留groupby的feature(如time_id)作为keys放入结果中,True是放,False是不放。 这也印证了帮助里的说明: group_keys : bool, optionalWhen calling apply and the by argument produces a like-indexed(i.e. :ref:a transform <groupby.transform>) result, add group keys toindex to identify pieces. By default group keys are not includedwhen the result’s index (and column) labels match the inputs, andare included otherwise. This argument has no effect if the result producedis 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:对英文帮助的深入理解,需要结合实际应用。
目录
- Pandas groupby方法的group_keys属性
- pandas版本1.5.3中groupby方法
- group_keys的意思是
- 总结
当设置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
是否保留groupby的feature(如time_id)作为keys放入结果中,True是放,False是不放。
这也印证了帮助里的说明:
group_keys : bool, optional
When calling apply and thebyargument 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:对英文帮助的深入理解,需要结合实际应用。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持风君子博客。
您可能感兴趣的文章:
- Pandas中groupby+agg的两种写法区别小结
- Python中pandas groupby()用法案例详解
- Python数据分析:pandas中Dataframe的groupby与索引用法
- Pandas分组函数groupby的用法详解
- pandas groupby()的使用小结
- Pandas Groupby之在Python中汇总、聚合和分组数据的示例详解