pandas-09 pd.groupby()的用法

在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}
'''

最新文章

  1. linux内核分析作业8:理解进程调度时机跟踪分析进程调度与进程切换的过程
  2. Linux 格式化扩展分区(Extended)
  3. 挖掘微信Web版通信的全过程 [转]
  4. Linux crontab定时执行任务
  5. DOM(一)模型中的模型节点
  6. springMVC 上传文件
  7. CalendarUtil
  8. iOS 转让APP
  9. 为什么我刚发表的文章变成了“待审核”,csdn有没有官方解释啊
  10. web框架--来自维基百科
  11. 7zS.sfx RunProgram with parameters
  12. nodejs学习第一天之模块
  13. Hibernate学习笔记(4)---hibernate的核心接口
  14. Netflix Recommendations
  15. vue-文字块收缩与展开功能
  16. shell-自动按省市建立文件夹,并在每个城市下创建当前日期文件夹
  17. PDF裁剪页面,PDF怎么裁剪页面的方法
  18. classfication中使用图像金字塔和sliding windows提高准确率
  19. tcpdump使用方法总结
  20. 集合框架二(Collection接口实现类常用遍历方法)

热门文章

  1. Gradle插件和Gradle对应表
  2. servlet是什么?servlet到底是啥?
  3. MySQL 设计与开发规范
  4. 如何使用phantomJS来模拟一个HTML元素的鼠标悬停
  5. Selenium+Java完整框架搭建(2019完整版)
  6. python bottle + jieba分词服务
  7. Java13新特性 -- 文本块
  8. openvswitch2.11.0修改源码后重新编译(2)
  9. java html实体转义
  10. 【翻译】Flink Table Api &amp; SQL — 性能调优 — 流式聚合