I have a dataframe like below.
df=pd.DataFrame({ 'month' : [1,2,3,4,5,6],
'temp' : [50,60,40,np.nan,np.nan,np.nan]})
df
Output:
month temp
0 1 50.0
1 2 60.0
2 3 40.0
3 4 NaN
4 5 NaN
5 6 NaN
I want to fill missing value using rolling average of previous two values. Now, in case of index 4(or month 5), I want to use calculated rolling mean at index 3 and index 2 value. So,
- temp at index 3: 50 (~ avg(60,40))
- temp at index 4: 45 (~ avg(40,50))
Expected output:
month temp
0 1 50.0
1 2 60.0
2 3 40.0
3 4 50.0
4 5 45.0
5 6 47.5
I checked the documentation but there does not seem a way to do that. Any work around? Thank you.