Python Pandas分組聚合的實(shí)現(xiàn)方法
Pycharm 鼠標(biāo)移動(dòng)到函數(shù)上,CTRL+Q可以快速查看文檔,CTR+P可以看基本的參數(shù)。
apply(),applymap()和map()
apply()和applymap()是DataFrame的函數(shù),map()是Series的函數(shù)。
apply()的操作對象是DataFrame的一行或者一列數(shù)據(jù),applymap()是DataFrame的每一個(gè)元素。map()也是Series中的每一個(gè)元素。
apply()對dataframe的內(nèi)容進(jìn)行批量處理, 這樣要比循環(huán)來得快。如df.apply(func,axis=0,.....) func:定義的函數(shù),axis=0時(shí)為對列操作,=1時(shí)為對行操作。
map()和python內(nèi)建的沒啥區(qū)別,如df['one'].map(sqrt)。
import numpy as np from pandas import Series, DataFrame frame = DataFrame(np.random.randn(4, 3), columns = list('bde'), index = ['Utah', 'Ohio', 'Texas', 'Oregon']) print frame print np.abs(frame) print f = lambda x: x.max() - x.min() print frame.apply(f) print frame.apply(f, axis = 1) def f(x): return Series([x.min(), x.max()], index = ['min', 'max']) print frame.apply(f) print print 'applymap和map' _format = lambda x: '%.2f' % x print frame.applymap(_format) print frame['e'].map(_format)
Groupby
Groupby是Pandas中最為常用和有效的分組函數(shù),有sum()、count()、mean()等統(tǒng)計(jì)函數(shù)。
groupby 方法返回的 DataFrameGroupBy 對象實(shí)際并不包含數(shù)據(jù)內(nèi)容,它記錄的是df['key1'] 的中間數(shù)據(jù)。當(dāng)你對分組數(shù)據(jù)應(yīng)用函數(shù)或其他聚合運(yùn)算時(shí),pandas 再依據(jù) groupby 對象內(nèi)記錄的信息對 df 進(jìn)行快速分塊運(yùn)算,并返回結(jié)果。
df = DataFrame({'key1': ['a', 'a', 'b', 'b', 'a'], 'key2': ['one', 'two', 'one', 'two', 'one'], 'data1': np.random.randn(5), 'data2': np.random.randn(5)}) grouped = df.groupby(df['key1']) print grouped.mean() df.groupby(lambda x:'even' if x%2==0 else 'odd').mean() #通過函數(shù)分組
聚合agg()
對于分組的某一列(行)或者多個(gè)列(行,axis=0/1),應(yīng)用agg(func)可以對分組后的數(shù)據(jù)應(yīng)用func函數(shù)。例如:用grouped['data1'].agg('mean')也是對分組后的'data1'列求均值。當(dāng)然也可以同時(shí)作用于多個(gè)列(行)和使用多個(gè)函數(shù)上。
df = DataFrame({'key1': ['a', 'a', 'b', 'b', 'a'], 'key2': ['one', 'two', 'one', 'two', 'one'], 'data1': np.random.randn(5), 'data2': np.random.randn(5)}) grouped = df.groupby('key1') print grouped.agg('mean') data1 data2 key1 a 0.749117 0.220249 b -0.567971 -0.126922
apply()和agg()功能上差不多,apply()常用來處理不同分組的缺失數(shù)據(jù)的填充和top N的計(jì)算,會(huì)產(chǎn)生層級索引。
而agg可以同時(shí)傳入多個(gè)函數(shù),作用于不同的列。
df = DataFrame({'key1': ['a', 'a', 'b', 'b', 'a'], 'key2': ['one', 'two', 'one', 'two', 'one'], 'data1': np.random.randn(5), 'data2': np.random.randn(5)}) grouped = df.groupby('key1') print grouped.agg(['sum','mean']) print grouped.apply(np.sum) #apply的在這里同樣適用,只是不能傳入多個(gè),這兩個(gè)函數(shù)基本是可以通用的。
data1 data2
sum mean sum mean
key1
a 2.780273 0.926758 -1.561696 -0.520565
b -0.308320 -0.154160 -1.382162 -0.691081
data1 data2 key1 key2
key1
a 2.780273 -1.561696 aaa onetwoone
b -0.308320 -1.382162 bb onetwo
apply和agg功能上基本是相近的,但是多個(gè)函數(shù)的時(shí)候還是agg比較方便。
apply本身的自由度很高,如果分組之后不做聚合操作緊緊是一些觀察的時(shí)候,apply就有用武之地了。
print grouped.apply(lambda x: x.describe()) data1 data2 key1 a count 3.000000 3.000000 mean -0.887893 -1.042878 std 0.777515 1.551220 min -1.429440 -2.277311 25% -1.333350 -1.913495 50% -1.237260 -1.549679 75% -0.617119 -0.425661 max 0.003021 0.698357 b count 2.000000 2.000000 mean -0.078983 0.106752 std 0.723929 0.064191 min -0.590879 0.061362 25% -0.334931 0.084057 50% -0.078983 0.106752 75% 0.176964 0.129447 max 0.432912 0.152142
此外apply還能改變返回?cái)?shù)據(jù)的維度。
http://pandas.pydata.org/pandas-docs/stable/groupby.html
此外還有透視表pivot_table ,交叉表crosstab ,但是我沒用過。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PyTorch中tensor.backward()函數(shù)的詳細(xì)介紹及功能實(shí)現(xiàn)
backward()?函數(shù)是PyTorch框架中自動(dòng)求梯度功能的一部分,它負(fù)責(zé)執(zhí)行反向傳播算法以計(jì)算模型參數(shù)的梯度,這篇文章主要介紹了PyTorch中tensor.backward()函數(shù)的詳細(xì)介紹,需要的朋友可以參考下2024-02-02python中讀取txt文件時(shí)split()函數(shù)的妙用
這篇文章主要介紹了python中讀取txt文件時(shí)split()函數(shù)的妙用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11python利用7z批量解壓rar的實(shí)現(xiàn)
這篇文章主要介紹了python利用7z批量解壓rar的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08一起來學(xué)習(xí)一下python的數(shù)字類型
這篇文章主要為大家詳細(xì)介紹了python的數(shù)字類型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01python3通過gevent.pool限制協(xié)程并發(fā)數(shù)量的實(shí)現(xiàn)方法
這篇文章主要介紹了python3通過gevent.pool限制協(xié)程并發(fā)數(shù)量的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Python web框架(django,flask)實(shí)現(xiàn)mysql數(shù)據(jù)庫讀寫分離的示例
這篇文章主要介紹了Python web框架(django,flask)實(shí)現(xiàn)mysql數(shù)據(jù)庫讀寫分離的示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-11-11