在R中,您可以计算具有指定窗口的滚动平均值,该窗口每次可以移动指定的量
然而,也许我在任何地方都找不到它,但在pandas或其他Python库中似乎都找不到它
有人知道解决这个问题的方法吗?我给你举个例子来说明我的意思:
这里我们有两周的数据,我在计算两个月的移动平均值,移动1个月,也就是2行
所以在R中,我会做一些类似的事情:two_month__movavg=rollapply(mydata,4,mean,by = 2,na.pad = FALSE)
Python中没有等价物吗
编辑1:
DATE A DEMAND ... AA DEMAND A Price
0 2006/01/01 00:30:00 8013.27833 ... 5657.67500 20.03
1 2006/01/01 01:00:00 7726.89167 ... 5460.39500 18.66
2 2006/01/01 01:30:00 7372.85833 ... 5766.02500 20.38
3 2006/01/01 02:00:00 7071.83333 ... 5503.25167 18.59
4 2006/01/01 02:30:00 6865.44000 ... 5214.01500 17.53
您可以再次使用滚动,只需要一点工作就可以分配索引
这里
by = 2
所以,我知道这个问题已经问了很长时间了,因为我碰到了同样的问题,在处理长时间序列时,你真的希望避免对你不感兴趣的值进行不必要的计算。由于该方法不实现
step
参数,所以我使用numpy编写了一个变通方法它基本上是this link中的解决方案和BENY提出的索引的组合
如果数据大小不太大,以下是一种简单的方法:
相关问题 更多 >
编程相关推荐