Missing values in Time Series in python

后端 未结 4 1887
暗喜
暗喜 2021-02-07 20:42

I have a time series dataframe, the dataframe is quite big and contain some missing values in the 2 columns(\'Humidity\' and \'Pressure\'). I would like to impute this missing v

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-07 21:02

    You could use rolling like this:

    frame = pd.DataFrame({'Humidity':np.arange(50,64)})
    
    frame.loc[[3,7,10,11],'Humidity'] = np.nan
    
    frame.Humidity.fillna(frame.Humidity.rolling(4,min_periods=1).mean())
    

    Output:

    0     50.0
    1     51.0
    2     52.0
    3     51.0
    4     54.0
    5     55.0
    6     56.0
    7     55.0
    8     58.0
    9     59.0
    10    58.5
    11    58.5
    12    62.0
    13    63.0
    Name: Humidity, dtype: float64
    

提交回复
热议问题