Average value around a point with an arbitrary step

Rabbit :

everybody. I have a pandas DataFrame containing about 5 million rows, one of the columns contains information about the electrical signal. I would like to calculate the average value around each point with a step forward and backward, for example, for a point with an index of 10, calculate the average between (8, 9, 11, 12).

I know that there is a rolling method, but it takes values from the point only forward. I don 't want to write a loop.

Jaroslav Bezděk :

You can achieve this by setting center parameter in pandas.DataFrame.rolling() method to True. As documentation says:

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.

Here is a simple example:

>>> 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291677&siteId=1