演示:
设定:
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1223 次 |
| 最近记录: |