為了能更好地處理數(shù)值型數(shù)據(jù),Pandas 提供了幾種窗口函數(shù),比如移動(dòng)函數(shù)(rolling)、擴(kuò)展函數(shù)(expanding)和指數(shù)加權(quán)函數(shù)(ewm)。
窗口函數(shù)應(yīng)用場(chǎng)景非常多。舉一個(gè)簡(jiǎn)單的例子:現(xiàn)在有 10 天的銷售額,而您想每 3 天求一次銷售總和,也就說(shuō)第五天的銷售額等于(第三天 + 第四天 + 第五天)的銷售額之和,此時(shí)窗口函數(shù)就派上用場(chǎng)了。
窗口是一種形象化的叫法,這些函數(shù)在執(zhí)行操作時(shí),就如同窗口一樣在數(shù)據(jù)區(qū)間上移動(dòng)。
本節(jié)學(xué)習(xí)主要講解如何在 DataFrame 和 Series 對(duì)象上應(yīng)用窗口函數(shù)。
rolling() 又稱移動(dòng)窗口函數(shù),它可以與 mean、count、sum、median、std 等聚合函數(shù)一起使用。為了使用方便,Pandas 為移動(dòng)函數(shù)定義了專門的方法聚合方法,比如 rolling_mean()、rolling_count()、rolling_sum() 等。其的語(yǔ)法格式如下:
rolling(window=n, min_periods=None, center=False)
常用參數(shù)說(shuō)明如下:
參數(shù)名稱 | 說(shuō)明 |
---|---|
window | 默認(rèn)值為 1,表示窗口的大小,也就是觀測(cè)值的數(shù)量, |
min_periods | 表示窗口的最小觀察值,默認(rèn)與 window 的參數(shù)值相等。 |
center | 是否把中間值做為窗口標(biāo)準(zhǔn),默認(rèn)值為 False。 |
下面看一組示例:
import pandas as pd
import numpy as np
#生成時(shí)間序列
df = pd.DataFrame(np.random.randn(8, 4),index = pd.date_range('12/1/2020', periods=8),columns = ['A', 'B', 'C', 'D'])
print(df)
#每3個(gè)數(shù)求求一次均值
print(df.rolling(window=3).mean())
輸出結(jié)果:
A B C D 2020-12-01 0.580058 -0.715246 0.440427 -1.106783 2020-12-02 -1.313982 0.068954 -0.906665 1.382941 2020-12-03 0.349844 -0.549509 -0.806577 0.261794 2020-12-04 -0.497054 0.921995 0.232008 -0.815291 2020-12-05 2.658108 0.447783 0.049340 0.329209 2020-12-06 -0.271670 -0.070299 0.860684 -0.095122 2020-12-07 -0.706780 -0.949392 0.679680 0.230930 2020-12-08 0.027379 -0.056543 -1.067625 1.386399 A B C D 2020-12-01 NaN NaN NaN NaN 2020-12-02 NaN NaN NaN NaN 2020-12-03 -0.128027 -0.398600 -0.424272 0.179317 2020-12-04 -0.487064 0.147147 -0.493745 0.276481 2020-12-05 0.836966 0.273423 -0.175076 -0.074763 2020-12-06 0.629794 0.433160 0.380677 -0.193734 2020-12-07 0.559886 -0.190636 0.529901 0.155006 2020-12-08 -0.317024 -0.358745 0.157580 0.507402
window=3
表示是每一列中依次緊鄰的每 3 個(gè)數(shù)求一次均值。當(dāng)不滿足 3 個(gè)數(shù)時(shí),所求值均為 NaN 值,因此前兩列的值為 NaN,直到第三行值才滿足要求 window =3。求均值的公式如下所示:
(index1+index2+index3)/3
expanding() 又叫擴(kuò)展窗口函數(shù),擴(kuò)展是指由序列的第一個(gè)元素開始,逐個(gè)向后計(jì)算元素的聚合值。
下面示例,min_periods = n表示向后移動(dòng) n 個(gè)值計(jì)求一次平均值:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2018', periods=10),
columns = ['A', 'B', 'C', 'D'])
print (df.expanding(min_periods=3).mean())
輸出結(jié)果:
A B C D 2020-01-01 NaN NaN NaN NaN 2020-01-02 NaN NaN NaN NaN 2020-01-03 -0.567833 0.258723 0.498782 0.403639 2020-01-04 -0.384198 -0.093490 0.456058 0.459122 2020-01-05 -0.193821 0.085318 0.389533 0.552429 2020-01-06 -0.113941 0.252397 0.214789 0.455281 2020-01-07 0.147863 0.400141 -0.062493 0.565990 2020-01-08 -0.036038 0.452132 -0.091939 0.371364 2020-01-09 -0.043203 0.368912 -0.033141 0.328143 2020-01-10 -0.100571 0.349378 -0.078225 0.225649
設(shè)置 min_periods=3,表示至少 3 個(gè)數(shù)求一次均值,計(jì)算方式為 (index0+index1+index2)/3,而 index3 的計(jì)算方式是 (index0+index1+index2+index3)/3,依次類推。
ewm(全稱 Exponentially Weighted Moving)表示指數(shù)加權(quán)移動(dòng)。ewn() 函數(shù)先會(huì)對(duì)序列元素做指數(shù)加權(quán)運(yùn)算,其次計(jì)算加權(quán)后的均值。該函數(shù)通過(guò)指定 com、span 或者 halflife 參數(shù)來(lái)實(shí)現(xiàn)指數(shù)加權(quán)移動(dòng)。示例如下:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('12/1/2020', periods=10),
columns = ['A', 'B', 'C', 'D'])
#設(shè)置com=0.5,先加權(quán)再求均值
print(df.ewm(com=0.5).mean())
輸出結(jié)果:
A B C D 2020-12-01 -1.511428 1.427826 0.252652 0.093601 2020-12-02 -1.245101 -0.118346 0.170232 -0.207065 2020-12-03 0.131456 -0.271979 -0.679315 -0.589689 2020-12-04 -0.835228 0.094073 -0.973924 -0.081684 2020-12-05 1.279812 1.099368 0.203033 0.019014 2020-12-06 0.132027 -0.625744 -0.145090 -0.318155 2020-12-07 0.820230 0.371620 0.119683 -0.227101 2020-12-08 1.088283 -0.275570 0.358557 -1.050606 2020-12-09 0.538304 -1.288146 0.590358 -0.164057 2020-12-10 0.589177 -1.514472 -0.613158 0.367322
在數(shù)據(jù)分析的過(guò)程中,使用窗口函數(shù)能夠提升數(shù)據(jù)的準(zhǔn)確性,并且使數(shù)據(jù)曲線的變化趨勢(shì)更加平滑,從而讓數(shù)據(jù)分析變得更加準(zhǔn)確、可靠。
更多建議: