本文介绍了是否可以使用步长大于 1 的 pandas.DataFrame.rolling ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在 R 中,您可以使用指定的窗口计算滚动平均值,该窗口每次可以移动指定的量.

In R you can compute a rolling mean with a specified window that can shift by a specified amount each time.

不过,也许我只是没有在任何地方找到它,但您似乎无法在 Pandas 或其他 Python 库中找到它?

However maybe I just haven't found it anywhere but it doesn't seem like you can do it in pandas or some other Python library?

有没有人知道解决这个问题的方法?我会给你一个例子来说明我的意思:

Does anyone know of a way around this? I'll give you an example of what I mean:

这里我们有两周一次的数据,我计算的是两个月移动平均线,该移动平均线移动了 1 个月,即 2 行.

Here we have bi-weekly data, and I am computing the two month moving average that shifts by 1 month which is 2 rows.

所以在 R 我会做这样的事情:two_month__movavg=rollapply(mydata,4,mean,by = 2,na.pad = FALSE) Python 中没有等价物吗?

So in R I would do something like: two_month__movavg=rollapply(mydata,4,mean,by = 2,na.pad = FALSE) Is there no equivalent in 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

推荐答案

你可以再次使用rolling,只需要一点点工作和你分配索引

You can using rolling again, just need a little bit work with you assign index

这里 by = 2

by = 2

df.loc[df.index[np.arange(len(df))%by==1],'New']=df.Price.rolling(window=4).mean()
df
    Price    New
0      63    NaN
1      92    NaN
2      92    NaN
3       5  63.00
4      90    NaN
5       3  47.50
6      81    NaN
7      98  68.00
8     100    NaN
9      58  84.25
10     38    NaN
11     15  52.75
12     75    NaN
13     19  36.75

这篇关于是否可以使用步长大于 1 的 pandas.DataFrame.rolling ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 16:17