【问题标题】:Rolling Window In Pandas - ExplanationPandas 中的滚动窗口 - 解释
【发布时间】:2018-01-28 19:00:11
【问题描述】:

我正在尝试学习 Python 的 Pandas 库,然后我遇到了用于时间序列分析的“滚动窗口”的概念。我从来都不是统计学的好学生,所以我有点迷茫。

请解释一下这个概念,最好用一个简单的例子,也许是一个代码sn-p。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    演示:

    设置:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-17
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2019-02-25
      • 1970-01-01
      相关资源
      最近更新 更多