根据 Python 中发生 NaN 的时间,通过“填充”和“插值”填充 NaN

我想根据 NaN 发生的时间使用“mean”和“interpolate”在 df 中填充 NaN。如下所示,第一个 NaN 发生在上午 6 点,第二个 NaN 发生在上午 8 点。


02/03/2016 05:00    8

02/03/2016 06:00    NaN

02/03/2016 07:00    1

02/03/2016 08:00    NaN

02/03/2016 09:00    3

我的 df 由数千天组成。我想对早上 7 点之前发生的任何 NaN 应用“填充”,并对早上 7 点之后发生的应用“插值”。我的数据是从早上 6 点到下午 6 点。


我的尝试是:


df_imputed = (df.between_time("00:00:00", "07:00:00", include_start=True, include_end=False)).ffill()

df_imputed = (df.between_time("07:00:00", "18:00:00", include_start=True, include_end=True)).interpolate()   

但它将我的 df 缩减到指定的时间段,而不是按照我的意愿填充 NaN。


编辑:我的 df 包含大约 400 列,因此该过程将适用于所有列。


婷婷同学_
浏览 200回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python