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中的字典与成员运算符初步探究
Oct 13 Python
python的mysqldb安装步骤详解
Aug 14 Python
flask + pymysql操作Mysql数据库的实例
Nov 13 Python
python代码过长的换行方法
Jul 19 Python
在python中利用KNN实现对iris进行分类的方法
Dec 11 Python
Python使用pyshp库读取shapefile信息的方法
Dec 29 Python
Python assert语句的简单使用示例
Jul 28 Python
wxpython实现按钮切换界面的方法
Nov 19 Python
PyTorch中的padding(边缘填充)操作方式
Jan 03 Python
利用python实现逐步回归
Feb 24 Python
Pytorch十九种损失函数的使用详解
Apr 29 Python
Keras多线程机制与flask多线程冲突的解决方案
May 28 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判断网络文件是否存在的方法
2015/03/12 PHP
php开发中的页面跳转方法总结
2015/04/26 PHP
深入浅析用PHP实现MVC
2016/03/02 PHP
php处理带有中文URL的方法
2016/07/11 PHP
Laravel框架实现修改登录和注册接口数据返回格式的方法
2018/08/17 PHP
PHP常量define和const的区别详解
2019/05/18 PHP
jQuery Ajax 全解析
2009/02/08 Javascript
为你的网站增加亮点的9款jQuery插件推荐
2011/05/03 Javascript
JavaScript设计模式经典之命令模式
2016/02/24 Javascript
再次谈论Javascript中的this
2016/06/23 Javascript
JS把内容动态插入到DIV的实现方法
2016/07/19 Javascript
jQuery实现简易的输入框字数计数功能示例
2017/01/16 Javascript
jQuery中的deferred使用方法
2017/03/27 jQuery
微信小程序 自定义Toast实例代码
2017/06/12 Javascript
Node做中转服务器转发接口
2017/10/18 Javascript
Rollup处理并打包JS文件项目实例代码
2018/05/31 Javascript
Vue中import from的来源及省略后缀与加载文件夹问题
2020/02/09 Javascript
[25:59]Newbee vs TNC 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python中的函数用法入门教程
2014/09/02 Python
Python 使用os.remove删除文件夹时报错的解决方法
2017/01/13 Python
pycharm的console输入实现换行的方法
2019/01/16 Python
Python3中_(下划线)和__(双下划线)的用途和区别
2019/04/26 Python
通过PHP与Python代码对比的语法差异详解
2019/07/10 Python
在Django admin中编辑ManyToManyField的实现方法
2019/08/09 Python
python实现按日期归档文件
2021/01/30 Python
详解CSS透明opacity和IE各版本透明度滤镜filter的最准确用法
2016/12/20 HTML / CSS
css 如何让背景图片拉伸填充避免重复显示
2013/07/11 HTML / CSS
Stuart Weitzman欧盟:美国奢华鞋履品牌
2017/05/24 全球购物
台湾深度自由行旅游平台:Tripbaa趣吧
2017/10/10 全球购物
墨尔本最受欢迎的复古风格品牌:Princess Highway
2018/12/21 全球购物
世界上最伟大的马产品:Equiderma
2020/01/07 全球购物
澳大利亚家具商店:Freedom
2020/12/17 全球购物
.NET面试题:什么是反射
2016/09/30 面试题
Python如何实现单例模式
2016/06/03 面试题
《石榴》教学反思
2014/03/02 职场文书
mybatis调用sqlserver存储过程返回结果集的方法
2021/05/08 SQL Server