python - Rolling Window In Pandas - Explanation -
i trying learn pandas library python, , came across concept of "rolling window" time-series analysis. have never been student of statistics, bit lost.
please explain concept, preferably using simple example, , maybe code snippet.
demo:
setup:
in [11]: df = pd.dataframe({'a':np.arange(10, 17)}) in [12]: df out[12]: 0 10 1 11 2 12 3 13 4 14 5 15 6 16
rolling sum 2 rows
window:
in [13]: df['a'].rolling(2).sum() out[13]: 0 nan # sum of current , previous value: 10 + nan = nan 1 21.0 # sum of current , previous value: 10 + 11 2 23.0 # sum of current , previous value: 11 + 12 3 25.0 # ... 4 27.0 5 29.0 6 31.0 name: a, dtype: float64
rolling sum 3 rows
window:
in [14]: df['a'].rolling(3).sum() out[14]: 0 nan # sum of current value , 2 preceeding rows: 10 + nan + nan 1 nan # sum of current value , 2 preceeding rows: 10 + 11 + nan 2 33.0 # sum of current value , 2 preceeding rows: 10 + 11 + 12 3 36.0 # ... 4 39.0 5 42.0 6 45.0 name: a, dtype: float64
Comments
Post a Comment