gpt4 book ai didi

python - 在滚动平均值中替换 NaN(python)

转载 作者:行者123 更新时间:2023-11-28 20:01:51 25 4
gpt4 key购买 nike

我有一个数据集如下:

ts
Out[227]:
Sales
Month
Jan 1808
Feb 1251
Mar 3023
Apr 4857
May 2506
Jun 2453
Jul 1180
Aug 4239
Sep 1759
Oct 2539
Nov 3923
Dec 2999

取window=2的移动平均后,输出为:

shifted = ts.shift(0)

window = shifted.rolling(window=2)

means = window.mean()

print(means)
Sales
Month
Jan NaN
Feb 1529.5
Mar 2137.0
Apr 3940.0
May 3681.5
Jun 2479.5
Jul 1816.5
Aug 2709.5
Sep 2999.0
Oct 2149.0
Nov 3231.0
Dec 3460.5

我希望将 NaN 替换为其原始值。可以吗?

最佳答案

试试这个:

In [92]: ts.rolling(window=2, min_periods=1).mean()
Out[92]:
Sales
Jan 1808.0
Feb 1529.5
Mar 2137.0
Apr 3940.0
May 3681.5
Jun 2479.5
Jul 1816.5
Aug 2709.5
Sep 2999.0
Oct 2149.0
Nov 3231.0
Dec 3461.0

关于python - 在滚动平均值中替换 NaN(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49406432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com