【问题标题】:Filtering DataFrame by column value in comparison to dictionary value?与字典值相比,按列值过滤DataFrame?
【发布时间】:2021-12-08 23:25:59
【问题描述】:

我有一个 DataFrame,为了简单起见,假设它只有两列: “创建日期”和“负责任的市场”。我还有一本名为 launch_dates 的字典,其键是与“负责任市场”列对应的国家/地区,其值是该国家/地区推出产品的日期。

我现在想过滤 DataFrame,以便只存在“创建日期”在其负责市场之外的发布日期之后的行。

我试过这个:

df = df.loc[df['Case created on - Date'] >= launch_dates.get(df.loc['Responsible Market'])]

但它给了我

KeyError: 'Responsible Market'

我的错误在哪里?

【问题讨论】:

    标签: python pandas filtering


    【解决方案1】:

    使用Series.map:

    df = df.loc[df['Case created on - Date'] >= df['Responsible Market'].map(launch_dates)]
    

    【讨论】:

      猜你喜欢
      • 2016-05-13
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2016-08-21
      • 2015-12-22
      相关资源
      最近更新 更多