Python Pandas聚合函數的應用示例

Python Pandas聚合函數

在前一節,我們重點介紹瞭窗口函數。我們知道,窗口函數可以與聚合函數一起使用,聚合函數指的是對一組數據求總和、最大值、最小值以及平均值的操作,本節重點講解聚合函數的應用。

應用聚合函數

首先讓我們創建一個 DataFrame 對象,然後對聚合函數進行應用。

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 4),index = pd.date_range('12/14/2020', periods=5),columns = ['A', 'B', 'C', 'D'])
print (df)
#窗口大小為3,min_periods 最小觀測值為1
r = df.rolling(window=3,min_periods=1)
print(r) 

輸出結果:

                   A         B         C         D
2020-12-14  0.941621  1.205489  0.473771 -0.348169
2020-12-15 -0.276954  0.076387  0.104194  1.537357
2020-12-16  0.582515  0.481999 -0.652332 -1.893678
2020-12-17 -0.286432  0.923514  0.285255 -0.739378
2020-12-18  2.063422 -0.465873 -0.946809  1.590234

Rolling [window=3,min_periods=1,center=False,axis=0]

1) 對整體聚合

您可以把一個聚合函數傳遞給 DataFrame,示例如下:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 4),index = pd.date_range('12/14/2020', periods=5),columns = ['A', 'B', 'C', 'D'])
print (df)
#窗口大小為3,min_periods 最小觀測值為1
r = df.rolling(window=3,min_periods=1)
#使用 aggregate()聚合操作
print(r.aggregate(np.sum))

輸出結果:

             A         B         C         D
2020-12-14  0.133713  0.746781  0.499385  0.589799
2020-12-15 -0.777572  0.531269  0.600577 -0.393623
2020-12-16  0.408115 -0.874079  0.584320  0.507580
2020-12-17 -1.033055 -1.185399 -0.546567  2.094643
2020-12-18  0.469394 -1.110549 -0.856245  0.260827

                   A         B         C         D
2020-12-14  0.133713  0.746781  0.499385  0.589799
2020-12-15 -0.643859  1.278050  1.099962  0.196176
2020-12-16 -0.235744  0.403971  1.684281  0.703756
2020-12-17 -1.402513 -1.528209  0.638330  2.208601
2020-12-18 -0.155546 -3.170027 -0.818492  2.863051

2) 對任意某一列聚合

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 4),index = pd.date_range('12/14/2020', periods=5),columns = ['A', 'B', 'C', 'D'])
#窗口大小為3,min_periods 最小觀測值為1
r = df.rolling(window=3,min_periods=1)
#對 A 列聚合
print(r['A'].aggregate(np.sum))

輸出結果:

2020-12-14    1.051501
2020-12-15    1.354574
2020-12-16    0.896335
2020-12-17    0.508470
2020-12-18    2.333732
Freq: D, Name: A, dtype: float64

3) 對多列數據聚合

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 4),index = pd.date_range('12/14/2020', periods=5),columns = ['A', 'B', 'C', 'D'])
#窗口大小為3,min_periods 最小觀測值為1
r = df.rolling(window=3,min_periods=1)
#對 A/B 兩列聚合
print(r['A','B'].aggregate(np.sum))

輸出結果:

                  A         B
2020-12-14  0.639867 -0.229990
2020-12-15  0.352028  0.257918
2020-12-16  0.637845  2.643628
2020-12-17  0.432715  2.428604
2020-12-18 -1.575766  0.969600

4) 對單列應用多個函數

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 4),index = pd.date_range('12/14/2020', periods=5),columns = ['A', 'B', 'C', 'D'])
#窗口大小為3,min_periods 最小觀測值為1
r = df.rolling(window=3,min_periods=1)
#對 A/B 兩列聚合
print(r['A','B'].aggregate([np.sum,np.mean]))

輸出結果:

                 sum      mean
2020-12-14 -0.469643 -0.469643
2020-12-15 -0.626856 -0.313428
2020-12-16 -1.820226 -0.606742
2020-12-17 -2.007323 -0.669108
2020-12-18 -0.595736 -0.198579

5) 對不同列應用多個函數

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 4),
   index = pd.date_range('12/11/2020', periods=5),
   columns = ['A', 'B', 'C', 'D'])
r = df.rolling(window=3,min_periods=1)
print( r['A','B'].aggregate([np.sum,np.mean]))

輸出結果:

                   A                   B         
                 sum      mean       sum      mean
2020-12-14 -1.428882 -1.428882 -0.417241 -0.417241
2020-12-15 -1.315151 -0.657576 -1.580616 -0.790308
2020-12-16 -2.093907 -0.697969 -2.260181 -0.753394
2020-12-17 -1.324490 -0.441497 -1.578467 -0.526156
2020-12-18 -2.400948 -0.800316 -0.452740 -0.150913

6) 對不同列應用不同函數

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(3, 4),
    index = pd.date_range('12/14/2020', periods=3),
    columns = ['A', 'B', 'C', 'D'])
r = df.rolling(window=3,min_periods=1)
print(r.aggregate({'A': np.sum,'B': np.mean}))

輸出結果:

                A         B
2020-12-14  0.503535 -1.301423
2020-12-15  0.170056 -0.550289
2020-12-16 -0.086081 -0.140532

總結

到此這篇關於Python Pandas聚合函數的文章就介紹到這瞭,更多相關Python Pandas聚合函數內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: