【问题标题】:Pandas : Find rows of a Dataframe that are not in another DataFrame [duplicate]Pandas:查找不在另一个DataFrame中的DataFrame行[重复]
【发布时间】:2019-10-09 03:57:22
【问题描述】:

我有两个 Pandas 数据框 df1df2,其中 df2df1 的一部分,我想创建一个数据框 df3,其中包含来自 df1 的所有行不在df2.

这是一个例子:

print(df1)

>>
+---------+
|       ID|
+---------+
|      AAA|
|      DDD|
|      BBB|
|      CCC|
|      EEE|
|      FFF|
+---------+

print(df2)

>>
+---------+
|       ID|
+---------+
|      AAA|
|      EEE|
|      FFF|
+---------+

print(df3)

>>
+---------+
|       ID|
+---------+
|      DDD|
|      BBB|
|      CCC|
+---------+

注意:

  • 我的 DataFrame 可能有多个列,但必须仅在 ID 列上进行匹配。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:
    df3 = df1.loc[~df1['ID'].isin(df2['ID'])].copy()
    

    【讨论】:

    • 你能在上面的代码中添加一些细节吗?
    猜你喜欢
    • 2018-03-19
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多