python - 具有任意步长的点周围的平均值

标签 python pandas

大家。我有一个包含大约 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/

相关文章:

python - C 库的 python 包装的可维护性

python - 在 scrapy 的管道中注入(inject)参数

python - 优化后我的 Sprite 不再显示了。为什么我的 Sprite 没有显示在我的 pygame 代码中?

python - 组合 Pandas 中的行

python - 如何在pandas中处理这个逻辑

python - 从python中的字符串中删除前导文本字符

python - 为什么在排序索引上进行索引查找时出现 PerformanceWarning?

python - 显示半小时间隔的数据框列

Python 请求——尽管使用流式上传仍出现 MemoryError

python - 解释字符串模式中使用反斜杠的 perl split