自动插值 pandas 数据帧中的缺失值



我有一个数据框,其中包含过去一年特定始发地和目的地的航空公司预订数据。系统中有数百个类似的数据集。

在每个数据集中,数据中都有漏洞。在当前示例中,我们一年中大约有 85 天没有预订数据。

这里有两列 - departure_date and bookings.

我的下一步将是to include the missing dates in the date column, and set the corresponding values in bookings column to NaN.

我正在寻找最好的方法来做到这一点。

请在下面找到数据帧的一部分:

Index       departure_date              bookings
0           2017-11-02 00:00:00             43
1           2017-11-03 00:00:00             27
2           2017-11-05 00:00:00             27 ********
3           2017-11-06 00:00:00             22
4           2017-11-07 00:00:00             39
.
.
164         2018-05-22 00:00:00             17
165         2018-05-23 00:00:00             41
166         2018-05-24 00:00:00             73
167         2018-07-02 00:00:00             4  *********
168         2018-07-03 00:00:00             31
.
.
277         2018-10-31 00:00:00             50
278         2018-11-01 00:00:00             60

我们可以看到数据集是一年的时间段(2017 年 11 月 2 日至 2018 年 11 月 1 日)。但我们只有279天的数据。例如,我们没有 2018-05-25 和 2018-07-01 之间的任何数据。我必须在departure_date列中包含这些日期,并将相应的预订值设置为 NaN。

对于第二步,我计划使用类似的东西进行一些插值

dataFrame['bookings'].interpolate(method='time', inplace=True)

请建议 Python 中是否有更好的替代方案。

每天重新采样。然后填补空白。

dataFrame['bookings'].resample('D').pad()

您可以在此页面上有更多重采样器的想法(因此您可以选择最适合您需求的一个):https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html

最新更新