使用熊猫移动平均2天时间

问题描述

我有一个数据框df,我想用窗口5来计算接下来2天的移动平均值。然后,我希望将结果存储在Python中的变量中:

 date                count

 08052020            50
 08152020            10
 08252020            30
 09052020            10
 09152020            15
 09252020            5

这就是我在做什么:

 count = df['new'] = df['count'].rolling(2).mean()
 print(count)

任何见识都会受到赞赏。

已更新所需的输出

 count = [23,14]
       

         

解决方法

我认为您需要:

df['new'] = df['count'].rolling(5).mean()

count = df["new"].dropna().to_list()

print(count)

输出:

[23.0,14.0]