I want to calculate the centered rolling average in Pandas, where the center value is excluded.
The code below works perfectly, but it includes the center value:
df.Value.rolling(window=11, center=True, min_periods=1).mean()
The window=11 and min_periods=1 are important, since I want to calculate the average for every value in df.
For example,
- for the second value, it should consider 5 values below and 1 that is above the current value. (Current code includes the second value)
- for the last value, it should consider 5 values above, but exclude the last one. (Current code considers the last 6 values)
Here is a table with the expected results:
| Values | Expected |
|---|---|
| 13313 | 27180.6 |
| 12792 | 28897 |
| 20254 | 28770.14286 |
| 34915 | 27468.5 |
| 31410 | 29037 |
| 36532 | 30028.5 |
| 36958 | 34071.1 |
| 35471 | 36600.66667 |
| 33018 | 38950.625 |
| 38080 | 38804 |
| 44074 | 39037.33333 |
| 54165 | 37520.2 |
And calculation method from Excel:
updated answer
You can modify my first approach of the initial answer to have a dynamic division:
Output:
initial answer
You have different ways, whether or not the operation is really a mean:
Output:
Or with numpy's
sliding_window_view:Output: