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中的CURL PycURL使用例子
Jun 01 Python
跟老齐学Python之模块的加载
Oct 24 Python
Python中使用异常处理来判断运行的操作系统平台方法
Jan 22 Python
Python的组合模式与责任链模式编程示例
Feb 02 Python
python的random模块及加权随机算法的python实现方法
Jan 04 Python
Python使用numpy实现BP神经网络
Mar 10 Python
Python合并多个Excel数据的方法
Jul 16 Python
python绘制多个曲线的折线图
Mar 23 Python
Python for循环与getitem的关系详解
Jan 02 Python
python torch.utils.data.DataLoader使用方法
Apr 02 Python
python如何解析复杂sql,实现数据库和表的提取的实例剖析
May 15 Python
python中remove函数的踩坑记录
Jan 04 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
几款免费开源的不用数据库的php的cms
2010/12/19 PHP
PHP在字符断点处截断文字的实现代码
2011/04/21 PHP
MySQL 日期时间函数常用总结
2012/06/12 PHP
php出现web系统多域名登录失败的解决方法
2014/09/30 PHP
PHP简单日历实现方法
2016/07/20 PHP
php is_executable判断给定文件名是否可执行实例
2016/09/26 PHP
微信公众号开发客服接口实例代码
2016/10/21 PHP
Laravel框架实现调用百度翻译API功能示例
2019/05/30 PHP
javascript编程起步(第四课)
2007/01/10 Javascript
JavaScript的变量作用域深入理解
2009/10/25 Javascript
js中判断文本框是否为空的两种方法
2011/07/31 Javascript
JavaScript分秒倒计时器实现方法
2015/02/02 Javascript
Node.js 异步异常的处理与domain模块解析
2017/05/10 Javascript
详解原生js实现offset方法
2017/06/15 Javascript
实例解析ES6 Proxy使用场景介绍
2018/01/08 Javascript
vue.js实现点击后动态添加class及删除同级class的实现代码
2018/04/04 Javascript
浅谈Vue.js路由管理器 Vue Router
2018/08/16 Javascript
vue利用v-for嵌套输出多层对象,分别输出到个表的方法
2018/09/07 Javascript
小程序click-scroll组件设计
2019/06/18 Javascript
layui使用form表单实现post请求页面跳转的方法
2019/09/14 Javascript
vue keep-alive的简单总结
2021/01/25 Vue.js
深入剖析Python的爬虫框架Scrapy的结构与运作流程
2016/01/20 Python
python实现TF-IDF算法解析
2018/01/02 Python
Python进阶之尾递归的用法实例
2018/01/31 Python
Django项目中model的数据处理以及页面交互方法
2018/05/30 Python
Python常见MongoDB数据库操作实例总结
2018/07/24 Python
在Django中URL正则表达式匹配的方法
2018/12/20 Python
Python3.5常见内置方法参数用法实例详解
2019/04/29 Python
TensorFlow学习之分布式的TensorFlow运行环境
2020/02/05 Python
在python中使用nohup命令说明
2020/04/16 Python
学校联谊活动方案
2014/02/15 职场文书
2014优秀大学生简历自我评价
2014/09/15 职场文书
2015年初中元旦晚会活动总结
2014/11/28 职场文书
地道战观后感2000字
2015/06/04 职场文书
React Hook用法示例详解(6个常见hook)
2021/04/28 Javascript
MySQL不使用order by实现排名的三种思路总结
2021/06/02 MySQL