自动插入熊猫数据框中的缺失值

时间:2019-01-04 10:22:08

标签: python pandas time-series interpolation missing-data

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

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

这里有两列-departure_date and bookings.

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

我正在寻找最好的方法。

请在下面找到dataFrame的一部分:

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之间没有任何数据。我必须将这些日期包括在离场日期列中,并将相应的预订值设置为NaN。

第二步,我打算使用类似的方法进行插值

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

请提出在Python中是否还有更好的替代方法。

1 个答案:

答案 0 :(得分:1)

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

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

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