fiendnick fiendnick - 21 days ago 5
Python Question

pandas Series: calculate means between neighbours

I have pandas Series and want to compute means between elements that are neighbours.

For example

[1 2 3 4 5 6 7 8 9]
would give the result
[1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5]

Answer

Try using rolling for this:

S = pd.Series(range(1,10))
S1 = S.rolling(2).mean().dropna()

Output:

1    1.5
2    2.5
3    3.5
4    4.5
5    5.5
6    6.5
7    7.5
8    8.5
Comments