简体   繁体   中英

How to procure moving average on data while keeping 'NaN' values in mind?

I have the following data which can contain NaN values and I want to calculate the rolling mean but it should ignore the NaN values.

Date        Price
01-03-2020 100.0
02-03-2020 200.0
03-03-2020  NaN
04-03-2020  NaN
05-03-2020  NaN
06-03-2020  NaN
07-03-2020  NaN
08-03-2020  100.0
09-03-2020  300.0
10-03-2020  NaN  

After df.rolling(3,on='Date').mean() I am getting output

    Date        Price
    01-03-2020  NaN
    02-03-2020  NaN
    03-03-2020  NaN
    04-03-2020  NaN
    05-03-2020  NaN
    06-03-2020  NaN
    07-03-2020  NaN
    08-03-2020  NaN
    09-03-2020  NaN
    10-03-2020  NaN

The output I want:

    Date        Price
    03-03-2020  150.0
    04-03-2020  200.0
    05-03-2020  NaN
    06-03-2020  NaN
    07-03-2020  NaN
    08-03-2020  100.0
    09-03-2020  200.0
    10-03-2020  200.0

You can use np.nanmean()

df.rolling(3,on='Date').apply(lambda x : np.nanmean(x))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM