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基础教程之对象和类的实际运用
Aug 29 Python
Python的SQLalchemy模块连接与操作MySQL的基础示例
Jul 11 Python
Golang与python线程详解及简单实例
Apr 27 Python
tensorflow学习笔记之简单的神经网络训练和测试
Apr 15 Python
python使用matplotlib画饼状图
Sep 25 Python
Python设计模式之简单工厂模式实例详解
Jan 22 Python
python创建属于自己的单词词库 便于背单词
Jul 30 Python
python队列原理及实现方法示例
Nov 27 Python
python 多进程和协程配合使用写入数据
Oct 30 Python
PyQt5中QSpinBox计数器的实现
Jan 18 Python
Python关于OS文件目录处理的实例分享
May 23 Python
使用pandas生成/读取csv文件的方法实例
Jul 09 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
模拟OICQ的实现思路和核心程序(一)
2006/10/09 PHP
php 什么是PEAR?
2009/03/19 PHP
网页游戏开发入门教程二(游戏模式+系统)
2009/11/02 PHP
JS父页面与子页面相互传值方法
2014/03/05 Javascript
Javascript基础知识(二)事件
2014/09/29 Javascript
javascript实现复制与粘贴操作实例
2014/10/16 Javascript
jQuery常用数据处理方法小结
2015/02/20 Javascript
Bootstrap导航栏各元素操作方法(表单、按钮、文本)
2015/12/28 Javascript
jQuery实现的纵向下拉菜单实例详解【附demo源码下载】
2016/07/09 Javascript
Vue组件化通讯的实例代码
2017/06/23 Javascript
基于JavaScript实现图片连播和联级菜单实例代码
2017/07/28 Javascript
JS运动特效之完美运动框架实例分析
2018/01/24 Javascript
深入浅析Vue中的 computed 和 watch
2018/06/06 Javascript
基于form-data请求格式详解
2019/10/29 Javascript
Vue中错误图片的处理的实现代码
2019/11/07 Javascript
javascript实现前端成语点击验证
2020/06/24 Javascript
python3实现短网址和数字相互转换的方法
2015/04/28 Python
python编写简单爬虫资料汇总
2016/03/22 Python
对sklearn的使用之数据集的拆分与训练详解(python3.6)
2018/12/14 Python
python3实现在二叉树中找出和为某一值的所有路径(推荐)
2019/12/26 Python
Python语言异常处理测试过程解析
2020/01/08 Python
Python轻量级web框架bottle使用方法解析
2020/06/13 Python
html5本地存储_动力节点Java学院整理
2017/07/12 HTML / CSS
便利店的创业计划书
2014/01/15 职场文书
九年级数学教学反思
2014/02/02 职场文书
数控专业个人求职信范文
2014/02/05 职场文书
法律进社区实施方案
2014/03/21 职场文书
酒店节能减排方案
2014/05/26 职场文书
励志演讲稿大全
2014/08/21 职场文书
师德师风自查总结
2014/10/14 职场文书
党的作风建设心得体会
2014/10/22 职场文书
劳动仲裁撤诉申请书
2015/05/18 职场文书
Python天气语音播报小助手
2021/09/25 Python
Spring Boot实战解决高并发数据入库之 Redis 缓存+MySQL 批量入库问题
2022/02/12 Redis
Golang入门之计时器
2022/05/04 Golang
MySQL 数据库 增删查改、克隆、外键 等操作
2022/05/11 MySQL