【问题标题】:check if all IDs are present in another dataset or not [duplicate]检查所有ID是否都存在于另一个数据集中[重复]
【发布时间】:2021-11-08 20:26:30
【问题描述】:

我有一个看起来像这样的 df1:

ParentID   Name
12         kids
29         jdö

第二个 df2 如下所示:

ParentID   Location
34         56789
12         5608

我想检查 df1 中的所有 parentID 是否都存在于 df2 中。如果没有,我想将它们提取到一个新的 df 中,例如:

ParentID   Name
29         jdö

我相信可以使用联接,但我不确定如何使用

result = left.join(right, on=["key1", "key2"], how="inner")

【问题讨论】:

标签: python python-3.x pandas dataframe join


【解决方案1】:

使用indicator=参数,然后过滤第一个数据框:

x = df1[df1.merge(df2, how="left", indicator=True)._merge.eq("left_only")]
print(x)

打印:

   ParentID Name
1        29  jdö

【讨论】:

  • 我认为这不准确。因为它会打印某些 parentID,当我在第二个数据集中手动检查它们时,它们就存在
猜你喜欢
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 2017-04-16
  • 2020-12-27
  • 2012-12-12
  • 2019-04-08
相关资源
最近更新 更多