How to fill nan values with rolling mean in pandas How to fill nan values with rolling mean in pandas pandas pandas

How to fill nan values with rolling mean in pandas


This should work:

input_data_frame[var_list]= input_data_frame[var_list].fillna(pd.rolling_mean(input_data_frame[var_list], 6, min_periods=1))

Note that the window is 6 because it includes the value of NaN itself (which is not counted in the average). Also the other NaN values are not used for the averages, so if less that 5 values are found in the window, the average is calculated on the actual values.

Example:

df = {'a': [1, 1,2,3,4,5, np.nan, 1, 1, 2, 3, 4, 5, np.nan] }df = pd.DataFrame(data=df)print df      a0   1.01   1.02   2.03   3.04   4.05   5.06   NaN7   1.08   1.09   2.010  3.011  4.012  5.013  NaN

Output:

      a0   1.01   1.02   2.03   3.04   4.05   5.06   3.07   1.08   1.09   2.010  3.011  4.012  5.013  3.0


rolling_mean function has been modified in pandas. If you fill the entire dataset, you can use;

filled_dataset = dataset.fillna(dataset.rolling(6,min_periods=1).mean())