Pycharm 鼠標(biāo)移動(dòng)到函數(shù)上,CTRL+Q可以快速查看文檔,CTR+P可以看基本的參數(shù)。
apply(),applymap()和map()
apply()和applymap()是DataFrame的函數(shù),map()是Series的函數(shù)。
apply()的操作對(duì)象是DataFrame的一行或者一列數(shù)據(jù),applymap()是DataFrame的每一個(gè)元素。map()也是Series中的每一個(gè)元素。
apply()對(duì)dataframe的內(nèi)容進(jìn)行批量處理, 這樣要比循環(huán)來得快。如df.apply(func,axis=0,.....) func:定義的函數(shù),axis=0時(shí)為對(duì)列操作,=1時(shí)為對(duì)行操作。
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 對(duì)象實(shí)際并不包含數(shù)據(jù)內(nèi)容,它記錄的是df['key1'] 的中間數(shù)據(jù)。當(dāng)你對(duì)分組數(shù)據(jù)應(yīng)用函數(shù)或其他聚合運(yùn)算時(shí),pandas 再依據(jù) groupby 對(duì)象內(nèi)記錄的信息對(duì) 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()
對(duì)于分組的某一列(行)或者多個(gè)列(行,axis=0/1),應(yīng)用agg(func)可以對(duì)分組后的數(shù)據(jù)應(yīng)用func函數(shù)。例如:用grouped['data1'].agg('mean')也是對(duì)分組后的'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)生層級(jí)索引。
而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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
