Pandas rolling mean in dataframe
Pandas
import pandas as pd
import numpy as np
Create a dataframe
df = pd.DataFrame({'B': range(5)})
df.index = [pd.Timestamp('20190101 09:00:00'),
pd.Timestamp('20190101 09:00:02'),
pd.Timestamp('20190101 09:00:03'),
pd.Timestamp('20190101 09:00:05'),
pd.Timestamp('20190101 09:00:06')]
df
B | |
---|---|
2019-01-01 09:00:00 | 0 |
2019-01-01 09:00:02 | 1 |
2019-01-01 09:00:03 | 2 |
2019-01-01 09:00:05 | 3 |
2019-01-01 09:00:06 | 4 |
Create a rolling dataframe mean by time interval
#here it will create a rolling mean of the previous two records in the dataframe
#e.g. (0, (0+1)/2, (1+2)/2, etc)
df.rolling(2, min_periods=1).mean()
B | |
---|---|
2019-01-01 09:00:00 | 0.0 |
2019-01-01 09:00:02 | 0.5 |
2019-01-01 09:00:03 | 1.5 |
2019-01-01 09:00:05 | 2.5 |
2019-01-01 09:00:06 | 3.5 |