I have a dataframe where the data progresses in one hour intervals but one of the columns has a few NaN values. When I encounter a NaN, I would like the code to average the four hours prior to the NaN and then replace the NaN with that average.
I tired modifying the answer to this question: pandas DataFrame: replace nan values with average of columns but this example is taking an average of the whole column and not part of the column.
My Dataframe:
0 1 2 ... 6 7 8
3 12:53 2.778 ... -12.2 -16.7 69%
4 11:53 3.611 ... NaN -17.2 73%
5 10:53 2.778 ... -13.7 -17.2 73%
6 09:53 3.611 ... -13.3 -17.2 73%
7 08:53 2.778 ... -12.8 -16.7 76%
…
74 13:53 0.278 ... -15 -17.2 83%
Here is my modified attempt at calling the last 4 in the column as follows:
for i in df.index:
df[6].fillna(df[6].iloc[0:3].mean(), inplace=True)
This is returning the first 4 rows of column 6 which makes sense why I am getting that result but I do not know how to have it call the 4 rows after the NaN and average them.