2017-08-20 81 views
2

我想學Python的熊貓圖書館,然後碰到時間序列分析的「滾動窗口」的概念。我從來沒有成爲統計學的好學生,所以我有點失落。大熊貓滾動窗口 - 說明

請解釋一下這個概念,最好使用一個簡單的例子,也許是一個代碼片段。

回答

4

演示:

設置:

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 [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 

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窗口滾動總和