I want to know how can I replace the NaN in my dataset with the last average of 5 last values.
| Column A | Column B |
|---|---|
| 1 | 2 |
| 2 | 5 |
| 3 | 5 |
| 4 | 2 |
| 5 | 2 |
| NaN | 2 |
| NaN | 2 |
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
| 1 | NaN |
| 1 | 2 |
| 1 | 2 |
For example, in this case the first NaN will be the average of (1,2,3,4,5) and second NaN will be the average of (2,3,4,5, The value of the other NaN).
I have tried
df.fillna(df.mean())