[英]Rolling Window In Pandas - Explanation
我試圖學習適用於Python的Pandas庫,然后遇到了“滾動窗口”的概念用於時間序列分析。 我從來都不是統計學的好學生,所以我有點迷茫。
請解釋一下這個概念,最好使用一個簡單的示例,也可能是一個代碼片段。
演示:
設定:
In [11]: df = pd.DataFrame({'a':np.arange(10, 17)})
In [12]: df
Out[12]:
a
0 10
1 11
2 12
3 13
4 14
5 15
6 16
2 rows
窗口的滾動總和:
In [13]: df['a'].rolling(2).sum()
Out[13]:
0 NaN # sum of the current and previous value: 10 + NaN = NaN
1 21.0 # sum of the current and previous value: 10 + 11
2 23.0 # sum of the current and previous value: 11 + 12
3 25.0 # ...
4 27.0
5 29.0
6 31.0
Name: a, dtype: float64
3 rows
窗口的滾動總和:
In [14]: df['a'].rolling(3).sum()
Out[14]:
0 NaN # sum of current value and two preceeding rows: 10 + NaN + Nan
1 NaN # sum of current value and two preceeding rows: 10 + 11 + Nan
2 33.0 # sum of current value and two preceeding rows: 10 + 11 + 12
3 36.0 # ...
4 39.0
5 42.0
6 45.0
Name: a, dtype: float64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.