【问题标题】:I have a all the rows with a particular column with lists. Select rows that does not contain atleast one element from the target list我有一个带有列表的特定列的所有行。从目标列表中选择不包含至少一个元素的行
【发布时间】:2019-08-14 16:27:24
【问题描述】:

我有一个数据框,其中的行包含列表(让我们调用 B) 我有一个目标列表(我们称之为 A)。我想存储所有在 B 和 A 中没有至少一个共同元素的行。

A = [
'IAB24',
'IAB9-WS1',
'IAB9-WS2',
'IAB26-WS1',
'IAB9-9',
'IAB14-WS1',
'IAB14-1',
'IAB19-15',
'IAB25-5',
'IAB25-2',
'IAB19-WS2',
'IAB26',
'IAB25-3',
'IAB7-39']

下面是 B 数据框:

URL          Category
google.com  [IAB19, Technology & Computing, 0.878928848558...
youtube.com [IAB25, Non-Standard Content, 0.99999999988656...
facebook.co [IAB14, Society, 0.974491504626058713, IAB14-W...
amazon.com  [IAB22, Shopping, 0.732955918165917875]
wpedia.org  [IAB5, Education, 1.000000000000000000]

我想检查 A 中的任何代码是否不存在于 B['Category'] 中,然后我想将它们存储在一个名为 C 的新数据框中。

【问题讨论】:

    标签: python pandas list apply


    【解决方案1】:

    您可以使用设置交集。我们想找到交集是空集的行。

    df[[not(bool(set(A) & set(x))) for x in df.Category]]
    

    更直接一点:

    df[[len(set(A) & set(x)) == 0 for x in df.Category]]
    

    【讨论】:

      猜你喜欢
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      相关资源
      最近更新 更多