您好,登錄后才能下訂單哦!
這篇文章主要介紹 Pandas如何使用GroupBy分組,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
import pandas as pd import numpy as np df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C' : np.random.randn(8), 'D' : np.random.randn(8)}) gb.groupby('A') print(df.groupby('A')) <pandas.core.groupby.DataFrameGroupBy object at 0x00000000042F3470> In [26]: gb.<TAB> gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight
In [41]: grouped = df.groupby('A') In [42]: for name, group in grouped: ....: print(name) ....: print(group) ....: bar A B C D1 bar one -0.042379 -0.0893293 bar three -0.009920 -0.9458675 bar two 0.495767 1.956030foo A B C D0 foo one -0.919854 -1.1313452 foo two 1.247642 0.3378634 foo two 0.290213 -0.9321326 foo one 0.362949 0.0175877 foo three 1.548106 -0.016692
In [44]: grouped.get_group('bar')Out[44]: A B C D1 bar one -0.042379 -0.0893293 bar three -0.009920 -0.9458675 bar two 0.495767 1.956030
In [56]: grouped = df.groupby('A')In [57]: grouped['C'].agg([np.sum, np.mean, np.std])Out[57]: sum mean stdA bar 0.443469 0.147823 0.301765foo 2.529056 0.505811 0.966450
In [60]: grouped.agg({'C' : np.sum, ....: 'D' : lambda x: np.std(x, ddof=1)}) ....: Out[60]: C D A bar 0.443469 1.490982foo 2.529056 0.645875
轉(zhuǎn)變函數(shù)(transform)中需要返回一個和分組塊(group chunk)同樣大小的結(jié)果,比如我們需要標(biāo)準(zhǔn)化每一個分組的數(shù)據(jù):
In [66]: index = pd.date_range('10/1/1999', periods=1100) In [67]: ts = pd.Series(np.random.normal(0.5, 2, 1100), index) In [68]: ts = ts.rolling(window=100,min_periods=100).mean().dropna() In [71]: key = lambda x: x.year#使用年來分組In [72]: zscore = lambda x: (x - x.mean()) / x.std()#標(biāo)準(zhǔn)化In [73]: transformed = ts.groupby(key).transform(zscore)#使用索引的年份來分組,然后標(biāo)準(zhǔn)化各組數(shù)據(jù)In [80]: compare = pd.DataFrame({'Original': ts, 'Transformed': transformed})# 做出圖形
filter方法返回一個子集(subset)。比如我們只想要組長度大于2的分組:
In [105]: dff = pd.DataFrame({'A': np.arange(8), 'B': list('aabbbbcc')}) In [106]: dff.groupby('B').filter(lambda x: len(x) > 2) Out[106]: A B2 2 b3 3 b4 4 b5 5 b
In [123]: df Out[123]: A B C D0 foo one -0.919854 -1.1313451 bar one -0.042379 -0.0893292 foo two 1.247642 0.3378633 bar three -0.009920 -0.9458674 foo two 0.290213 -0.9321325 bar two 0.495767 1.9560306 foo one 0.362949 0.0175877 foo three 1.548106 -0.016692In [124]: grouped = df.groupby('A')# could also just call .describe()In [125]: grouped['C'].apply(lambda x: x.describe()) Out[125]: A bar count 3.000000 mean 0.147823 std 0.301765 min -0.042379 25% -0.026149 50% -0.009920 75% 0.242924... foo mean 0.505811 std 0.966450 min -0.919854 25% 0.290213 50% 0.362949 75% 1.247642 max 1.548106Name: C, dtype: float64
以上是“ Pandas如何使用GroupBy分組”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。