pandas.apply()函數

一、介紹數據結構

apply函數是pandas裏面全部函數中自由度最高的函數。該函數以下:app

DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)dom

該函數最有用的是第一個參數,這個參數是函數,至關於C/C++的函數指針。函數

這個函數須要本身實現,函數的傳入參數根據axis來定,好比axis = 1,就會把一行數據做爲Series的數據 結構傳入給本身實現的函數中,咱們在函數中實現對Series不一樣屬性之間的計算,返回一個結果,則apply函數 會自動遍歷每一行DataFrame的數據,最後將全部結果組合成一個Series數據結構並返回。性能

二、樣例spa

import numpy as np import pandas as pd if __name__ == '__main__': f = lambda x : x.max() - x.min() df = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['utah', 'ohio', 'texas', 'oregon']) #columns表述列標, index表述行標
    print(df) t1 = df.apply(f) #df.apply(function, axis=0),默認axis=0,表示將一列數據做爲Series的數據結構傳入給定的function中
    print(t1) t2 = df.apply(f, axis=1) print(t2)

輸出結果以下所示:.net

 b d e utah 1.950737  0.318299  0.387724 ohio 1.584464 -0.082965  0.984757 texas 0.477283 -2.774454 -0.532181 oregon -0.851359 -0.654882  1.026698

b 2.802096 d 3.092753 e 1.558879 dtype: float64
utah
1.632438 ohio 1.667428 texas 3.251737 oregon 1.878057 dtype: float64

三、性能比較指針

import numpy as np import pandas as pd def my_test(a, b): return a + b if __name__ == '__main__': df = pd.DataFrame({'a':np.random.randn(6), 'b':['foo', 'bar'] * 3, 'c':np.random.randn(6)}) print(df) df['value1'] = df.apply(lambda row: my_test(row['a'], row['c']), axis=1) print(df) df['vaule2'] = df['a'] + df['c'] print(df)

輸出結果以下:code

 a b c 0 -1.745471  foo  0.723341
1 -0.378998  bar  0.229188
2 -1.468866  foo  0.788046
3 -1.323347  bar  0.323051
4 -1.894372  foo  2.216768
5 -0.649059  bar  0.858149

a b c value1 0 -1.745471 foo 0.723341 -1.022130 1 -0.378998 bar 0.229188 -0.149810 2 -1.468866 foo 0.788046 -0.680820 3 -1.323347 bar 0.323051 -1.000296 4 -1.894372 foo 2.216768 0.322396 5 -0.649059 bar 0.858149 0.209089

a b c value1 vaule2 0 -1.745471 foo 0.723341 -1.022130 -1.022130 1 -0.378998 bar 0.229188 -0.149810 -0.149810 2 -1.468866 foo 0.788046 -0.680820 -0.680820 3 -1.323347 bar 0.323051 -1.000296 -1.000296 4 -1.894372 foo 2.216768 0.322396 0.322396 5 -0.649059 bar 0.858149 0.209089 0.209089

注意:當數據量很大時,對於簡單的邏輯處理建議方法2(我的處理幾百M數據集時,方法1花時200s左右,方法2花時10s)!!!blog

相關文章
相關標籤/搜索