大家。我有一个包含大约 500 万行的 pandas DataFrame,其中一列包含有关电信号的信息。我想向前和向后计算每个点周围的平均值,例如,对于索引为10的点,计算(8,9,11,12)之间的平均值。
我知道有一种滚动方法,但它只从该点向前获取值。我不想写循环。
最佳答案
您可以通过在 pandas.DataFrame.rolling()
中设置 center
参数来实现此目的方法为True
。正如文档所述:
By default, the result is set to the right edge of the window. This can be changed to the center of the window by setting center=True.
这是一个简单的例子:
>>> serie = pd.Series(range(15))
>>> print(serie.rolling(5, center=True).mean())
0 NaN
1 NaN
2 2.0
3 3.0
4 4.0
5 5.0
6 6.0
7 7.0
8 8.0
9 9.0
10 10.0
11 11.0
12 12.0
13 NaN
14 NaN
关于python - 具有任意步长的点周围的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60706036/