当前位置: 首页 > news >正文

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               NaN1          0.0000002          0.0000003          0.0000004          0.000000...   
26454    5237975   -0.0012285237976    0.0004915237977   -0.0050315237978    0.0032195237979    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:对英文帮助的深入理解,需要结合实际应用。

http://www.lryc.cn/news/226108.html

相关文章:

  • win 命令替代鼠标的操作
  • Shopee活动取消规则是什么?shopee官方促销活动怎么取消?
  • 安卓常见设计模式2------构建者模式(Kotlin版)
  • redis主从复制+哨兵
  • html动态爱心超文本标记代码,丝滑流畅有特效,附源码
  • 力扣:162. 寻找峰值(Python3)
  • 【Python】20大报告生成词云
  • 目标检测YOLO实战应用案例100讲-基于无人机的轻量化目标检测系统设计
  • ansible-第二天
  • 【测试工具】UnixBench 测试
  • 软件测试金融项目,在测试的时候一定要避开的一些雷区
  • 顺序图——画法详解
  • easyexcel==省市区三级联动
  • Linux进程控制(二)--进程等待(一)
  • 【C++】C++11常用特性梳理
  • 修改iframe生成的pdf的比例
  • C++之list的用法介绍
  • Mybatis-plus 内部提供的 ServiceImpl<M extends BaseMapper<T>, T> 学习总结
  • yolov5 利用Labelimg对图片进行标注
  • 完整版付费进群带定位源码
  • 华为L410上制作内网镜像模板01
  • linuxC语言缓冲区及小程序的实现
  • MySQL数据库基本操作-DDL 数据库基础知识
  • 基于JavaWeb+SpringBoot+Vue摩托车商城微信小程序系统的设计和实现
  • idea代码快捷键Mac版
  • 分享76个Python管理系统源代码总有一个是你想要的
  • Springboot养老院信息管理系统的开发-计算机毕设 附源码27500
  • 在虚拟机中安装vim和net-tools,mysql
  • 【Excel】函数sumif范围中符合指定条件的值求和
  • k8s上对Pod的管理部分详解