pandas中pd.groupby()的用法詳解
在pandas中的groupby和在sql語(yǔ)句中的groupby有異曲同工之妙,不過(guò)也難怪,畢竟關(guān)系數(shù)據(jù)庫(kù)中的存放數(shù)據(jù)的結(jié)構(gòu)也是一張大表罷了,與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() 可以統(tǒng)計(jì)每個(gè)組 成員的 數(shù)量
'''
city
BJ ? ?6
GZ ? ?4
SH ? ?8
SZ ? ?2
dtype: int64
'''
print(g.get_group('BJ')) # 得到 某個(gè) 分組
'''
? ? ? ? ?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()) # 對(duì)這個(gè) 分組 求平均
'''
temperature ? ?10.000000
wind ? ? ? ? ? ?2.833333
dtype: float64
'''
# 直接使用 g 對(duì)象,求平均值
print(g.mean()) # 對(duì) 每一個(gè) 分組, 都計(jì)算分組
'''
? ? ? 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 對(duì)象還可以使用 for 進(jìn)行循環(huán)遍歷
for name, group in g:
? ? print(name)
? ? print(group)
# g 可以轉(zhuǎn)化為 list類型, dict類型
print(list(g)) # 元組第一個(gè)元素是 分組的label,第二個(gè)是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))) # 返回鍵值對(duì),值的類型是 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}
'''到此這篇關(guān)于pandas中pd.groupby()的用法詳解的文章就介紹到這了,更多相關(guān)pandas pd.groupby()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- pandas groupby()的使用小結(jié)
- Pandas實(shí)現(xiàn)groupby分組統(tǒng)計(jì)方法實(shí)例
- pandas中g(shù)roupby操作實(shí)現(xiàn)
- pandas中df.groupby()方法深入講解
- pandas?groupby?用法實(shí)例詳解
- Pandas數(shù)據(jù)分析之groupby函數(shù)用法實(shí)例詳解
- 詳解Pandas中GroupBy對(duì)象的使用
- Pandas實(shí)現(xiàn)groupby分組統(tǒng)計(jì)的實(shí)踐
- Pandas中GroupBy具體用法詳解
- Pandas分組函數(shù)groupby的用法詳解
相關(guān)文章
Python繪制數(shù)據(jù)動(dòng)態(tài)圖的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言繪制好看的數(shù)據(jù)動(dòng)態(tài)圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-07-07
使用 Celery Once 來(lái)防止 Celery 重復(fù)執(zhí)行同一個(gè)任務(wù)
這篇文章主要介紹了使用 Celery Once 來(lái)防止 Celery 重復(fù)執(zhí)行同一個(gè)任務(wù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
Python和JS反爬之解決反爬參數(shù)?signKey
這篇文章主要介紹了Python和JS反爬之解決反爬參數(shù)?signKey,Python?反爬中有一大類,叫做字體反爬,核心的理論就是通過(guò)字體文件或者?CSS?偏移,接下來(lái)文章的詳細(xì)介紹,需要的小伙伴可以參考一下2022-05-05
Python Pandas批量讀取csv文件到dataframe的方法
這篇文章主要介紹了Python Pandas批量讀取csv文件到dataframe的方法,需要的朋友可以參考下2018-10-10
python+tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的秒鐘
這篇文章主要為大家詳細(xì)介紹了Python如何利用tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的秒鐘,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以自己動(dòng)手嘗試一下2024-02-02

