Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas fillna and rolling mean

Tags:

python

pandas

I am trying to fill all missing values until the end of the dataframe but unable to do so. In the example below, I am taking average of the last three values. My code is only filling until 2017-01-10 whereas I want to fill until 2017-01-14. For 1/14, I want to use values from 11,12 & 13.Please help.

import pandas as pd

df = pd.DataFrame([
    {"ds":"2017-01-01","y":3},
    {"ds":"2017-01-02","y":4},
    {"ds":"2017-01-03","y":6},
    {"ds":"2017-01-04","y":2},
    {"ds":"2017-01-05","y":7},
    {"ds":"2017-01-06","y":9},
    {"ds":"2017-01-07","y":8},
    {"ds":"2017-01-08","y":2},
    {"ds":"2017-01-09"},
    {"ds":"2017-01-10"},
    {"ds":"2017-01-11"},
    {"ds":"2017-01-12"},
    {"ds":"2017-01-13"},
    {"ds":"2017-01-14"}
    ])

df["y"].fillna(df["y"].rolling(3,min_periods=1).mean(),axis=0,inplace=True)

Result:

           ds    y
0   2017-01-01  3.0
1   2017-01-02  4.0
2   2017-01-03  6.0
3   2017-01-04  2.0
4   2017-01-05  7.0
5   2017-01-06  9.0
6   2017-01-07  8.0
7   2017-01-08  2.0
8   2017-01-09  5.0
9   2017-01-10  2.0
10  2017-01-11  NaN
11  2017-01-12  NaN
12  2017-01-13  NaN
13  2017-01-14  NaN

Desired output:

Desired output:

like image 371
appdev1014 Avatar asked Nov 19 '25 20:11

appdev1014


1 Answers

You can iterate over the values in y and if a nan value is encountered, look at the 3 earlier values and use .at[] to set the mean of the 3 earlier values as the new value:

for index, value in df['y'].items():
    if np.isnan(value):
        df['y'].at[index] = df['y'].iloc[index-3: index].mean()
    

Resulting dataframe for the missing values:

7   2017-01-08  2.000000
8   2017-01-09  6.333333
9   2017-01-10  5.444444
10  2017-01-11  4.592593
11  2017-01-12  5.456790
12  2017-01-13  5.164609
13  2017-01-14  5.071331
like image 180
Sander van den Oord Avatar answered Nov 22 '25 10:11

Sander van den Oord



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!