文章目录

DataFrame.groupby()概览应用举例

概览内容参考自官方文档详情应用参考文档

DataFrame.groupby()概览

DataFrame.groupby(self, by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, observed=False, **kwargs)[source]Group DataFrame or Series using a mapper or by a Series of columns.A groupby operation involves some combination of splitting the object, applying a function, and combining the results. This can be used to group large amounts of data and compute operations on these groups.参数列表解析

by : mapping, function, label, or list of labels 映射关系, 函数, 标签或标签列表 Used to determine the groups for the groupby. If by is a function, it’s called on each value of the object’s index. If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups (the Series’ values are first aligned; see .align() method). If an ndarray is passed, the values are used as-is determine the groups. A label or list of labels may be passed to group by the columns in self. Notice that a tuple is interpreted as a (single) key.  axis : {0 or ‘index’, 1 or ‘columns’}, default 0 轴,0为横向的行, 1为竖向的列, 默认为0 Split along rows (0) or columns (1). 用于区分函数或方法是应用于行数据,还是列数据  level : int, level name, or sequence of such, default None 整型, 索引名, 默认是None If the axis is a MultiIndex (hierarchical), group by a particular level or levels. 多级索引的情况下, 函数或方法应用于哪一级的索引,  as_index : bool, default True 布尔型, 默认为True For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output.  sort : bool, default True 布尔型,是否排序, 默认为True Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. Groupby preserves the order of rows within each group.  group_keys : bool, default True 布尔型, 默认为True, 当调用apply方法时, 添加group keys 到index用于识别. When calling apply, add group keys to index to identify pieces.  squeeze : bool, default False 布尔型,是否压缩, 默认为False, 可以理解为降维 Reduce the dimensionality of the return type if possible, otherwise return a consistent type.  observed : bool, default False 布尔型, 观察者, 默认为False This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers.  New in version 0.23.0.  **kwargs Optional, only accepts keyword argument ‘mutated’ and is passed to groupby.

应用举例

talk is cheap, show me the code

In [1]: import pandas as pd

In [2]: import numpy as np

In [5]: df = pd.DataFrame([

...:     {'date': '2018-12-01', 'total': 105, 'total2': 133.03},

...:     {'date': '2018-12-01', 'total': 105, 'total2': 2032.13},

...:     {'date': '2018-12-01', 'total': 109, 'total2': 1312.32},

...:     {'date': '2018-12-01', 'total': 109, 'total2': 33120.23},

...:     {'date': '2018-12-02', 'total': 103, 'total2': 13123.23},

...:     {'date': '2018-12-02', 'total': 103, 'total2': 231232.13},

...:     {'date': '2018-12-02', 'total': 105, 'total2': 1310.32},

...:     {'date': '2018-12-03', 'total': 105, 'total2': 33403.23},

...:     {'date': '2018-12-03', 'total': 105, 'total2': 105.23},

...:     {'date': '2018-12-03', 'total': 102, 'total2': 23552.13},

...:     {'date': '2018-12-04', 'total': 102, 'total2': 10365.32},

...:     {'date': '2018-12-04', 'total': 106, 'total2': 30563.23},

...:     {'date': '2018-12-04', 'total': 106, 'total2': 130.33},

...:     {'date': '2018-12-04', 'total': 107, 'total2': 2042.13},

...:     {'date': '2018-12-03', 'total': 107, 'total2': 136.02},

...:     {'date': '2018-12-04', 'total': 107, 'total2': 3063.23},

...: ])

...:

In [8]: grouped = df.groupby(by=['date'])

In [12]: grouped.mean()

Out[12]:

total      total2

date

2018-12-01  107.000000   9149.4275

2018-12-02  103.666667  81888.5600

2018-12-03  104.750000  14299.1525

2018-12-04  105.600000   9232.8480

python的dataframe的groupby_python pandas.DataFrame.groupby()方法详解相关推荐

  1. Pandas中resample方法详解

    Pandas中resample方法详解 Pandas中的resample,重新采样,是对原样本重新处理的一个方法,是一个对常规时间序列数据重新采样和频率转换的便捷的方法.重新取样时间序列数据. 方便的 ...

  2. python 命令-python解析命令行参数的三种方法详解

    这篇文章主要介绍了python解析命令行参数的三种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 python解析命令行参数主要有三种方法: ...

  3. Python实现发送警告通知到企业微信方法详解

    常见的报警方式有:邮件,电话,短信,微信.本文将介绍如何利用Python发送警告通知到企业微信,文中的示例代码有一定的参考价值,感兴趣的可以了解一下.编程资料点击免费领取 目录 1. 新建应用 2. ...

  4. PyKivy入门教程:Kivy Python库的介绍、安装及使用方法详解

    PyKivy入门教程:Kivy Python库的介绍.安装及使用方法详解 Kivy是一个跨平台的Python GUI工具包,可以用于开发能够在多种设备和操作系统上运行的应用程序.Kivy支持触摸输入. ...

  5. python telnet线程锁_对python使用telnet实现弱密码登录的方法详解

    系统环境: 64位win7企业版 python2.7.10 2016.08.16修改内容: 1)read_until()函数是可以设置timeout的,之前不能获取到password之后的返回是因为调 ...

  6. python列转行函数_Python pandas 列转行操作详解(类似hive中explode方法)

    最近在工作上用到Python的pandas库来处理excel文件,遇到列转行的问题.找了一番资料后成功了,记录一下. 1. 如果需要爆炸的只有一列: df=pd.DataFrame({'A':[1,2 ...

  7. python语言格式化输出_Python format()格式化输出方法详解

    原标题:Python format()格式化输出方法详解 format() 方法的语法格式如下: str.format(args) 此方法中,str 用于指定字符串的显示样式:args 用于指定要进行 ...

  8. python的底层实现_Python底层封装实现方法详解

    这篇文章主要介绍了Python底层封装实现方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 事实上,python封装特性的实现纯属" ...

  9. python中backward_pytorch的梯度计算以及backward方法详解

    基础知识 tensors: tensor在pytorch里面是一个n维数组.我们可以通过指定参数reuqires_grad=True来建立一个反向传播图,从而能够计算梯度.在pytorch中一般叫做d ...

  10. python怎样实现封装_Python底层封装实现方法详解

    Python底层封装实现方法详解 这篇文章主要介绍了Python底层封装实现方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 事实上,pyth ...

最新文章

  1. 第二项目AIaisell(易销宝)
  2. linux溢出总结+windows aslr地址随机化绕过
  3. LNMP-源码 nginx ,keepalived
  4. 【足迹C++primer】52、,转换和继承虚函数
  5. 谷歌浏览器 转方向_转专业 | 希望你们能在岔路口选对自己的路
  6. mysql 偏移量是什么_数据块内部偏移量的基本计算方法
  7. tcpdump抓两个网卡的包_Linux下网络抓包命令tcpdump详解(在wireshark中看包)
  8. php 匹配非数字,正则表达式 - 获取匹配和非获取匹配
  9. 增量式速度pid调节策略_增量式PID是什么?不知道你就落伍了
  10. webpack基础和完整项目脚手架搭建教程-郭永峰-专题视频课程
  11. 近期币圈与美股的相关性
  12. 3dmax导出unity3d模型设置
  13. 实数系的基本定理_11、实数的连续性(1)
  14. html设置编剧,编剧必备之电影剧本创作六大基本步骤
  15. 使用squid内网代理百度地图
  16. win7无法连接虚拟磁盘服务器,win7系统电脑打开磁盘管理出现“无法连接虚拟磁盘服务”的解决方法...
  17. Adobe系列软件彻底清理方法(手动删除)
  18. 是如何通过阿里面试的?
  19. docker elasticsearch Exception in thread main java.nio.file.NoSuchFileException: /usr/share/elasti
  20. 【python】深浅拷贝

热门文章

  1. PMBOK - 项目管理知识体系 - 学习系列(1) -
  2. PHP正则表达式(是)
  3. webshell使用方法-防止DDOS脚本-暴力破解-自动劫持root密码并转发密码到邮箱
  4. nginx启动成功,web页面报错
  5. Spring中使用id和name的区别。
  6. C语言输入中10A20B,C语言实验报告第3章答案.doc
  7. buffer pool mysql_理解Mysql中的Buffer pool
  8. 【Shell】for循环
  9. JEECGJWT异常捕获强化处理
  10. React Native图片缓存解决方案