【问题标题】:Remove Words From A DF that appear in another DF (Pandas,Python3)从一个 DF 中删除出现在另一个 DF 中的单词(Pandas,Python3)
【发布时间】:2014-09-25 22:15:04
【问题描述】:

现在,我有一个这样的 DF1:

 Words              Words1                Words2  
 Shell Shocked      Big Bang Theory       Hot Potato 
 Falling Down       Tiger Blood           Blue Suede Shoe 
 A Big Window       Mouse Trap
 Look Around

我希望做的是做某种等效的 vlookup,并从 DF1 的列中删除所有出现在 DF2 中的单词,如下所示:

   KW
Shell Shocked
Tiger Blood 
Blue Suede Shoe
A Big Window

这样我就剩下...

   Words              Words1                Words2  
 Falling Down      Big Bang Theory        Hot Potato               
 Look Around       Mouse Trap

这是我尝试过的:

  DF1 = DF1[~DF1['Words'].isin(DF2)]
  DF1 = DF1[~DF1['Words1'].isin(DF2)]
  DF1 = DF1[~DF1['Words2'].isin(DF2)]

这一个实际上并没有过滤任何东西(也许我这样做不正确)。我也试过了:

 set_B = set(onlykw.itertuples(index=False))
 mask = [x not in set_B for x in A.itertuples(index=False)]

它只返回“True”(我在另一篇 SO 帖子中发现了这个,但我不确定它是如何工作的)。 有谁知道如何做到这一点?

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    KW 是 DF2 中的列名吗?如果是这样,那么你需要

    DF1 = DF1[~DF1['Words'].isin(DF2['KW'])]
    

    【讨论】:

    • 啊,就是这样!总是最简单的解决方案...谢谢:)。当 SO 让我接受时会接受
    • @user3682157 是的,我自己犯过这个错误已经够多次了。
    猜你喜欢
    • 2014-11-21
    • 2020-04-06
    • 2023-01-09
    • 1970-01-01
    • 2020-12-13
    • 2017-04-22
    • 2022-11-26
    • 2019-10-20
    • 2020-09-16
    相关资源
    最近更新 更多