【问题标题】:How to compare a dataframe with a list and remove all rows which contain values from the list [closed]如何将数据框与列表进行比较并从列表中删除所有包含值的行[关闭]
【发布时间】:2021-08-14 23:56:07
【问题描述】:

我有一个数据框,其中有一列 Lien,每个 row 都有一个字符串和一个字符串列表。

Date de publication   Lien   Titre
0   24/05/2021   string   string
1   21/05/2021   string   string
2   21/05/2021   string   string
3   21/05/2021   string   string
4   21/05/2021   string   string

我想将 Lien 列与列表进行比较,并创建一个新数据框或编辑初始数据框,其中包含列表中的值的所有行都已删除。

【问题讨论】:

    标签: python pandas list dataframe


    【解决方案1】:

    假设你有这个数据框:

      Date de publication     Lien   Titre
    0          24/05/2021  string1  string
    1          21/05/2021  string2  string
    2          21/05/2021  string3  string
    3          21/05/2021  string4  string
    4          21/05/2021  string5  string
    

    然后您可以使用.isin() 按列表过滤此数据框。例如:

    to_remove = ["string2", "string4"]
    
    print(df[~df["Lien"].isin(to_remove)])
    

    打印:

      Date de publication     Lien   Titre
    0          24/05/2021  string1  string
    2          21/05/2021  string3  string
    4          21/05/2021  string5  string
    

    【讨论】:

    • 感谢它完美运行!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2018-03-14
    相关资源
    最近更新 更多