用不正确的日期范围填充pandas groupby和零

时间:2018-02-16 13:13:24

标签: python pandas data-manipulation

我有这种形式的租车预订数据:

location  |  day_of_drive |  day_of_reservation  |  number_of_bookings
-------------------------------------------------------------------
foo       |  01-01-2015   |  24-12-2014          |  1
foo       |  01-01-2015   |  26-12-2014          |  1
foo       |  01-01-2015   |  29-12-2014          |  3
foo       |  01-01-2015   |  30-12-2014          |  2
foo       |  01-01-2015   |  31-12-2014          |  1
foo       |  02-01-2015   |  29-12-2014          |  2
foo       |  02-01-2015   |  31-12-2014          |  1
foo       |  02-01-2015   |  01-01-2015          |  1
bar       |  25-06-2016   |  03-07-2016          |  1
.
.
.

我想要的是填充此数据集以包含0次预订的日期。

数据框已经排序,首先是location,然后是day_of_drive,然后是day_of_reservation

我想要的是在此变量的第一个观察值之间使用day_of_reservation变量填充零值的有效方法,该变量对应于此 {{1}的第一次预订} / day_of_drive 对, location ,对于给定的一对 day_of_drive { {1}}本身。我遇到了使用取消堆叠的解决方案,然后使用location,然后堆叠备份,但我不认为我可以使用这些解决方案,因为每对day_of_drivefillna(0)都有不同的分钟我希望在两者之间填写最大日期。

期望的输出:

location

我觉得解决方案是day_of_drive

2 个答案:

答案 0 :(得分:2)

您可以将以下内容与set_indexgroupbyresample一起使用:

#First make sure day_of_reservation is a datetime dtype:
df['day_of_reservation'] = pd.to_datetime(df['day_of_reservation'])

df.set_index('day_of_reservation')\
  .groupby(['location','day_of_drive'], sort=False)['number_of_bookings']\
  .resample('D').asfreq().fillna(0)\
  .reset_index()

输出:

      location     day_of_drive day_of_reservation  number_of_bookings
0   foo           01-01-2015            2014-12-24                 1.0
1   foo           01-01-2015            2014-12-25                 0.0
2   foo           01-01-2015            2014-12-26                 1.0
3   foo           01-01-2015            2014-12-27                 0.0
4   foo           01-01-2015            2014-12-28                 0.0
5   foo           01-01-2015            2014-12-29                 3.0
6   foo           01-01-2015            2014-12-30                 2.0
7   foo           01-01-2015            2014-12-31                 1.0
8   foo           02-01-2015            2014-12-29                 2.0
9   foo           02-01-2015            2014-12-30                 0.0
10  foo           02-01-2015            2014-12-31                 1.0
11  foo           02-01-2015            2015-01-01                 1.0
12  bar           25-06-2016            2016-03-07                 1.0

答案 1 :(得分:1)

这应该这样做:

    private UITestControl FindHtmlEditField(string keyToSearch)
    {
        HtmlEdit editFields = new HtmlEdit(_browserWindow);
        var matchedField = editFields.FindMatchingControls().FirstOrDefault(item => item.GetProperty(HtmlEdit.PropertyNames.Id).ToString().ToLower().Contains(keyToSearch.ToLower()));
        if (matchedField == null)
            throw new System.Exception(string.Format("Can't find {0} field", keyToSearch));

        return matchedField;
    }