網站首頁 編程語言 正文
在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}
'''
原文鏈接:https://blog.csdn.net/missyougoon/article/details/84022999
相關推薦
- 2023-10-31 IP地址、網關、網絡/主機號、子網掩碼關系
- 2022-11-20 CPython?垃圾收集器檢測循環引用詳解_python
- 2022-05-21 C語言實現銀行管理系統(文件操作)_C 語言
- 2022-04-14 python實現購物車小程序_python
- 2022-02-19 成功解決ParserError: Error tokenizing data. C error: E
- 2022-03-26 Android?Studio中Logcat寫入和查看日志_Android
- 2023-05-17 Kotlin開發中open關鍵字與類名函數名和變量名的使用方法淺析_Android
- 2022-10-04 Redis分布式鎖之紅鎖的實現_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支