简体   繁体   中英

rolling mean values to fill NaN

I got this function for fill main_df 's NaN cells. After had calculate the progressive rolling mean from every row, I'd then pick the value from the mean stream at each level of NaNs in order to substitute with those NaNs in the main_df cells.

Here is my trial:

main_df = pd.DataFrame({'a':[1,1,2,3,4,56,6],'b':[2,3,2,np.nan,4,3,6],'c':[1,1,2,3,4,56,np.nan]})

for i in range(len(main_df)):
    main_df.iloc[i] = main_df.iloc[i].fillna(value=main_df.iloc[i].ewm(span=1).mean())

Any suggestions to let it works?

I had final doublcheck my function works: I got substitution of nan in 'c' with values of ema2 at same level of nan

main_df = pd.DataFrame({'a':[1,1,2,3,4,56,6,4],'b':[2,3,2,np.nan,4,3,6,1],'c_copy':[1,1,2,3,4,56,np.nan,1],'c':[1,1,2,3,4,56,np.nan,1]})
main_df['ema2_c']= main_df['c'].ewm(span=2).mean()
main_df['c'] = main_df['c'].fillna(value=main_df['c'].ewm(span=2).mean())

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