pandas中pd.groupby()的用法详解


Posted in Python onJune 16, 2022

在pandas中的groupby和在sql语句中的groupby有异曲同工之妙,不过也难怪,毕竟关系数据库中的存放数据的结构也是一张大表罢了,与dataframe的形式相似。

import numpy as np
import pandas as pd
from pandas import Series, DataFrame


df = pd.read_csv('./city_weather.csv')
print(df)
'''
          date city  temperature  wind
0   03/01/2016   BJ            8     5
1   17/01/2016   BJ           12     2
2   31/01/2016   BJ           19     2
3   14/02/2016   BJ           -3     3
4   28/02/2016   BJ           19     2
5   13/03/2016   BJ            5     3
6   27/03/2016   SH           -4     4
7   10/04/2016   SH           19     3
8   24/04/2016   SH           20     3
9   08/05/2016   SH           17     3
10  22/05/2016   SH            4     2
11  05/06/2016   SH          -10     4
12  19/06/2016   SH            0     5
13  03/07/2016   SH           -9     5
14  17/07/2016   GZ           10     2
15  31/07/2016   GZ           -1     5
16  14/08/2016   GZ            1     5
17  28/08/2016   GZ           25     4
18  11/09/2016   SZ           20     1
19  25/09/2016   SZ          -10     4
'''

g = df.groupby(df['city'])
# <pandas.core.groupby.groupby.DataFrameGroupBy object at 0x7f10450e12e8>

print(g.groups)

# {'BJ': Int64Index([0, 1, 2, 3, 4, 5], dtype='int64'),
# 'GZ': Int64Index([14, 15, 16, 17], dtype='int64'),
# 'SZ': Int64Index([18, 19], dtype='int64'),
# 'SH': Int64Index([6, 7, 8, 9, 10, 11, 12, 13], dtype='int64')}

print(g.size()) # g.size() 可以统计每个组 成员的 数量
'''
city
BJ    6
GZ    4
SH    8
SZ    2
dtype: int64
'''

print(g.get_group('BJ')) # 得到 某个 分组
'''
         date city  temperature  wind
0  03/01/2016   BJ            8     5
1  17/01/2016   BJ           12     2
2  31/01/2016   BJ           19     2
3  14/02/2016   BJ           -3     3
4  28/02/2016   BJ           19     2
5  13/03/2016   BJ            5     3
'''

df_bj = g.get_group('BJ')
print(df_bj.mean()) # 对这个 分组 求平均
'''
temperature    10.000000
wind            2.833333
dtype: float64
'''

# 直接使用 g 对象,求平均值
print(g.mean()) # 对 每一个 分组, 都计算分组
'''
      temperature      wind
city                       
BJ         10.000  2.833333
GZ          8.750  4.000000
SH          4.625  3.625000
SZ          5.000  2.500000
'''

print(g.max())
'''
            date  temperature  wind
city                               
BJ    31/01/2016           19     5
GZ    31/07/2016           25     5
SH    27/03/2016           20     5
SZ    25/09/2016           20     4
'''

print(g.min())
'''
            date  temperature  wind
city                               
BJ    03/01/2016           -3     2
GZ    14/08/2016           -1     2
SH    03/07/2016          -10     2
SZ    11/09/2016          -10     1
'''

# g 对象还可以使用 for 进行循环遍历
for name, group in g:
    print(name)
    print(group)


# g 可以转化为 list类型, dict类型
print(list(g)) # 元组第一个元素是 分组的label,第二个是dataframe
'''
[('BJ',          date city  temperature  wind
0  03/01/2016   BJ            8     5
1  17/01/2016   BJ           12     2
2  31/01/2016   BJ           19     2
3  14/02/2016   BJ           -3     3
4  28/02/2016   BJ           19     2
5  13/03/2016   BJ            5     3), 
('GZ',           date city  temperature  wind
14  17/07/2016   GZ           10     2
15  31/07/2016   GZ           -1     5
16  14/08/2016   GZ            1     5
17  28/08/2016   GZ           25     4), 
('SH',           date city  temperature  wind
6   27/03/2016   SH           -4     4
7   10/04/2016   SH           19     3
8   24/04/2016   SH           20     3
9   08/05/2016   SH           17     3
10  22/05/2016   SH            4     2
11  05/06/2016   SH          -10     4
12  19/06/2016   SH            0     5
13  03/07/2016   SH           -9     5), 
('SZ',           date city  temperature  wind
18  11/09/2016   SZ           20     1
19  25/09/2016   SZ          -10     4)]
'''
print(dict(list(g))) # 返回键值对,值的类型是 dataframe
'''
{'SH':           date city  temperature  wind
6   27/03/2016   SH           -4     4
7   10/04/2016   SH           19     3
8   24/04/2016   SH           20     3
9   08/05/2016   SH           17     3
10  22/05/2016   SH            4     2
11  05/06/2016   SH          -10     4
12  19/06/2016   SH            0     5
13  03/07/2016   SH           -9     5, 
'SZ':           date city  temperature  wind
18  11/09/2016   SZ           20     1
19  25/09/2016   SZ          -10     4, 
'GZ':           date city  temperature  wind
14  17/07/2016   GZ           10     2
15  31/07/2016   GZ           -1     5
16  14/08/2016   GZ            1     5
17  28/08/2016   GZ           25     4, 
'BJ':          date city  temperature  wind
0  03/01/2016   BJ            8     5
1  17/01/2016   BJ           12     2
2  31/01/2016   BJ           19     2
3  14/02/2016   BJ           -3     3
4  28/02/2016   BJ           19     2
5  13/03/2016   BJ            5     3}
'''

到此这篇关于pandas中pd.groupby()的用法详解的文章就介绍到这了,更多相关pandas pd.groupby()内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!


Tags in this post...

Python 相关文章推荐
使用python搭建Django应用程序步骤及版本冲突问题解决
Nov 19 Python
跟老齐学Python之Python安装
Sep 12 Python
Python for Informatics 第11章之正则表达式(二)
Apr 21 Python
python编程之requests在网络请求中添加cookies参数方法详解
Oct 25 Python
python xlsxwriter库生成图表的应用示例
Mar 16 Python
Anaconda入门使用总结
Apr 05 Python
Python实现随机漫步功能
Jul 09 Python
python 利用pandas将arff文件转csv文件的方法
Feb 12 Python
python远程连接MySQL数据库
Apr 19 Python
python使用socket 先读取长度,在读取报文内容示例
Sep 26 Python
Python过滤序列元素的方法
Jul 31 Python
python 装饰器重要在哪
Feb 14 Python
python中pd.cut()与pd.qcut()的对比及示例
Jun 16 #Python
Python自动操作神器PyAutoGUI的使用教程
Jun 16 #Python
python内置模块之上下文管理contextlib
Jun 14 #Python
Python时间操作之pytz模块使用详解
Django框架之路由用法
Jun 10 #Python
深入理解pytorch库的dockerfile
Jun 10 #Python
如何利用python实现列表嵌套字典取值
Jun 10 #Python
You might like
windows的文件系统机制引发的PHP路径爆破问题分析
2014/07/28 PHP
PIGCMS 如何关闭聊天机器人
2015/02/12 PHP
用php来限制每个ip每天浏览页面数量的实现思路
2015/02/24 PHP
基于PHP代码实现中奖概率算法可用于刮刮卡、大转盘等抽奖算法
2015/12/20 PHP
搭建自己的PHP MVC框架详解
2017/08/16 PHP
javascript中xml操作实现代码
2011/11/21 Javascript
如何用js控制frame的隐藏或显示的解决办法
2013/03/20 Javascript
JavaScript数组常用操作技巧汇总
2014/11/17 Javascript
javascript包装对象实例分析
2015/03/27 Javascript
jQuery+Ajax实现无刷新操作
2016/01/04 Javascript
Bootstrap 折叠(Collapse)插件用法实例详解
2016/06/01 Javascript
关于动态生成dom绑定事件失效的原因及解决方法
2016/08/06 Javascript
Vue组件BootPage实现简单的分页功能
2016/09/12 Javascript
JavaScript实现选中文字提示新浪微博分享效果
2017/06/15 Javascript
详解node字体压缩插件font-spider的用法
2018/09/28 Javascript
3分钟了解vue数据劫持的原理实现
2019/05/01 Javascript
关于layui时间回显问题的解决方法
2019/09/24 Javascript
JavaScript回调函数callback用法解析
2020/01/14 Javascript
vue+element实现图片上传及裁剪功能
2020/06/29 Javascript
原生js+canvas实现验证码
2020/11/29 Javascript
python备份文件以及mysql数据库的脚本代码
2013/06/10 Python
pyqt5简介及安装方法介绍
2018/01/31 Python
django+xadmin+djcelery实现后台管理定时任务
2018/08/14 Python
Python网页解析器使用实例详解
2020/05/30 Python
Python学习之os模块及用法
2020/06/03 Python
零基础学Python之前需要学c语言吗
2020/07/21 Python
python Cartopy的基础使用详解
2020/11/01 Python
如何设置PyCharm中的Python代码模版(推荐)
2020/11/20 Python
CSS3中的transform属性进行2D和3D变换的基本用法
2016/05/12 HTML / CSS
Foot Locker意大利官网:全球领先的运动鞋和服装零售商
2017/05/30 全球购物
计算机专业优秀大学生自我总结
2014/01/21 职场文书
家长写给老师的建议书
2014/03/13 职场文书
演讲稿祖国在我心中
2014/05/04 职场文书
会计专业毕业生自荐书
2014/06/25 职场文书
项目工作说明书
2014/07/29 职场文书
PyTorch中permute的使用方法
2022/04/26 Python