【问题标题】:changing position windowing result改变位置窗口结果
【发布时间】:2021-09-22 18:59:05
【问题描述】:

以下是滚动窗口求和的正常结果:

s = pd.Series(range(5))

s.rolling(window=2).sum()

Out[2]: 
0    NaN
1    1.0
2    3.0
3    5.0
4    7.0
dtype: float64

但是,我希望它是

Out[2]: 
0    1.0
1    3.0
2    5.0
3    7.0
4    NaN
dtype: float64

如何通过滚动窗口实现这一点?

提前谢谢你。

【问题讨论】:

  • s.rolling(window=2).sum().shift(-1)?
  • 谢谢!我不知道移位功能

标签: python pandas windowing


【解决方案1】:

让我们用shift来实现吧

window = 2
s.rolling(window = window).sum().shift(-window+1)
Out[270]: 
0    1.0
1    3.0
2    5.0
3    7.0
4    NaN
dtype: float64

【讨论】:

  • 谢谢!我不知道移位功能
猜你喜欢
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 2011-04-09
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
相关资源
最近更新 更多