Reputation: 899
This is my first question related to python/Panda. I'm a beginner, but I'll try to explain my hopefully easy question. I love the rolling window function and use it like this for now to calculate the mean:
import pandas as pd
import random as r
d = [r.random() for i in range(0,100)]
df = pd.DataFrame(d, columns=['Values'])
df['rolling mean'] = df['Values'].rolling(30).mean()
df['Values'] is a column with random floats (for test purposes). It is intended to write the rolling mean value of the column "Values" into the column "rolling mean". This works perfectly! It's pretty cool and super efficient for now. But I wonder if it is possible to consider something like future values (outside the window).
So my question is:
Is there a simple and efficient way to calculate the mean value with the coming / future values of the rolling column?
Something like:
> ( t-1 + t0 + t+1 ) / 3
Thank you, I have tried to find a solution in the API and on SO, but as a beginner of a language it is always difficult to find the right place for information.
David
Upvotes: 1
Views: 1211
Reputation: 9207
Solved it this way for only getting the next values:
s = 7
df['sum'] = df['number'].iloc[::-1].rolling(s).sum().iloc[::-1]
basically reversing the series (.iloc[::-1]
), do rolling operation and then reversing series again.
Upvotes: 0
Reputation: 36
Just to show what is already suggested by Ben.T
import pandas as pd
import random as r
d = [r.random() for i in range(0,100)]
df = pd.DataFrame(d, columns=['Values'])
s=10
df['rolling mean'] = df['Values'].shift(-s).rolling(2s+1).mean()
Upvotes: 1