带有 Pandas 的自定义窗口的滚动平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20249149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rolling mean with customized window with Pandas
提问by zsljulius
Is there a way to customize the window of the rolling_mean function?
有没有办法自定义rolling_mean函数的窗口?
data
1
2
3
4
5
6
7
8
Let's say the window is set to 2, that is to calculate the average of 2 datapoints before and after the obervation including the observation. Say the 3rd observation. In this case, we will have (1+2+3+4+5)/5 = 3. So on and so forth.
假设窗口设置为 2,即计算观察前后 2 个数据点的平均值,包括观察。说第三个观察。在这种情况下,我们将有(1+2+3+4+5)/5 = 3. 等等等等。
回答by unutbu
Compute the usual rolling mean with a forward (or backward) window and then use the shiftmethod to re-center it as you wish.
使用向前(或向后)窗口计算通常的滚动平均值,然后使用该shift方法根据需要重新居中。
data_mean = pd.rolling_mean(data, window=5).shift(-2)
If you want to average over 2 datapoints before and after the observation (for a total of 5 datapoints) then make the window=5.
如果您想对观察前后的 2 个数据点进行平均(总共 5 个数据点),则将window=5.
For example,
例如,
import pandas as pd
data = pd.Series(range(1, 9))
data_mean = pd.rolling_mean(data, window=5).shift(-2)
print(data_mean)
yields
产量
0 NaN
1 NaN
2 3
3 4
4 5
5 6
6 NaN
7 NaN
dtype: float64
As kadee points out, if you wish to center the rolling mean, then use
正如kadee 指出的那样,如果您希望将滚动平均值居中,请使用
pd.rolling_mean(data, window=5, center=True)
回答by Sarah
For more current version of Pandas (please see 0.23.4 documentation https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html), you don't have rolling_mean anymore. Instead, you will use
有关 Pandas 的更多当前版本(请参阅 0.23.4 文档https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html),您不再有 rolling_mean 了。相反,您将使用
DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None)
For your example, it will be:
对于您的示例,它将是:
df.rolling(5,center=True).mean()

