【问题标题】:pandas rolling window that includes 'n' preceding rows and 'm' following rows包含“n”前行和“m”后行的熊猫滚动窗口
【发布时间】:2020-12-30 23:36:21
【问题描述】:

在 pandas 中,我如何编写一个有前行和后行的 rolling window

例如,一个sql滚动窗口可以这样写:

sum(id_0) over (partition by id_1 order by id_2 rows between 3 preceding and 1 following)

我不知道如何在 pandas 中写这个。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以计算前 4 个值的滚动总和,然后使用 shift() 方法将这些值应用于前一行 - 例如:

    rolling_ds = dataset.rolling(4, min_periods=1).sum().shift(-1)
    

    可以在此处找到有关此方法的更深入的讨论:Calculating Rolling forward averages with pandas

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 2020-09-18
      • 2020-04-21
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      相关资源
      最近更新 更多