【问题标题】:Pandas - Filtering out dates by comparing two DataframesPandas - 通过比较两个数据框过滤掉日期
【发布时间】:2019-01-24 17:48:59
【问题描述】:

我有一个数据框,其中包含一个月中的天数列表,如下所示:

Dataframe name : df

date
2018-12-01
2018-12-03
2018-12-07
2018-12-25
2018-12-31

我有另一个带有假期列表的数据框:

Dataframe name : Holidays


date
2018-12-21
2018-12-25

我正在尝试使用以下方法过滤掉 df 中存在于 Holidays 中的日期:

df = df[~df['date'].isin(Holidays['date'])]

以上内容应从最终输出中排除2018-12-25,但我看到它仍然在最终数据框中显示2018-12-25

预期输出:

Dataframe : df

date
2018-12-01
2018-12-03
2018-12-07
2018-12-31

【问题讨论】:

  • 添加代码sn-p,方便调试和回答
  • 上面的代码应该可以工作。你能打印出df.dtypesHolidays.dtypes。如果它们是object,那么查看df.date.unique()Holiday.date.unique() 的输出也很有用,可能是空格问题。

标签: python pandas


【解决方案1】:

与您已经在做的事情相似,但又有所不同。我只会使用逻辑索引

idx = (df.date & Holidays.date)
df = df.loc[~idx, :]

【讨论】:

    猜你喜欢
    • 2019-12-20
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 2019-06-06
    • 2018-09-29
    • 2014-08-31
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多