【数据分析可视化】数据分组技术GroupBy

理解GroupBy

在这里插入图片描述
类似于数据库分组的
在这里插入图片描述

GroupBy操作和数据库类似

城市天气进行GroupBy操作

对group的单个列求平均值是Series
对group求平均值返回DataFrame

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
# 读入城市天气csv文件
df = pd.read_csv('/Users/bennyrhys/Desktop/数据分析可视化-数据集/homework/city_weather.csv')
df
datecitytemperaturewind
003/01/2016BJ85
117/01/2016BJ122
231/01/2016BJ192
314/02/2016BJ-33
428/02/2016BJ192
513/03/2016BJ53
627/03/2016SH-44
710/04/2016SH193
824/04/2016SH203
908/05/2016SH173
1022/05/2016SH42
1105/06/2016SH-104
1219/06/2016SH05
1303/07/2016SH-95
1417/07/2016GZ102
1531/07/2016GZ-15
1614/08/2016GZ15
1728/08/2016GZ254
1811/09/2016SZ201
1925/09/2016SZ-104
# 按照城市来GroupBy
g = df.groupby(df['city'])
g
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x11a393610>
# 展示处理完会返回哪几个Group
g.groups
{'BJ': Int64Index([0, 1, 2, 3, 4, 5], dtype='int64'),
 'GZ': Int64Index([14, 15, 16, 17], dtype='int64'),
 'SH': Int64Index([6, 7, 8, 9, 10, 11, 12, 13], dtype='int64'),
 'SZ': Int64Index([18, 19], dtype='int64')}
# get_group相当于根据某列的分组过滤出来
df_bj = g.get_group('BJ')
df_bj
datecitytemperaturewind
003/01/2016BJ85
117/01/2016BJ122
231/01/2016BJ192
314/02/2016BJ-33
428/02/2016BJ192
513/03/2016BJ53
# 过滤求平均值
df_bj.mean()
temperature    10.000000
wind            2.833333
dtype: float64
# 过滤的均值 Series
type(df_bj.mean())
pandas.core.series.Series
# 按照过滤一组求平均值的思想,将4组group求平均值组合
# 支持Group直接求平均值
g.mean()
temperaturewind
city
BJ10.0002.833333
GZ8.7504.000000
SH4.6253.625000
SZ5.0002.500000
g.max()
datetemperaturewind
city
BJ31/01/2016195
GZ31/07/2016255
SH27/03/2016205
SZ25/09/2016204
g.min()
datetemperaturewind
city
BJ03/01/2016-32
GZ14/08/2016-12
SH03/07/2016-102
SZ11/09/2016-101
g
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x11a393610>
# 将DataFrameGroupBy -> list 字典
list(g)
[('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)]
dict(list(g))
{'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}
dict(list(g))['BJ']
datecitytemperaturewind
003/01/2016BJ85
117/01/2016BJ122
231/01/2016BJ192
314/02/2016BJ-33
428/02/2016BJ192
513/03/2016BJ53
for name, group_df in g:
    print(name)
    print(group_df)
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
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞 新

请小哥喝杯茶~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值