如果一列的字符串包含熊猫数据框中另一列的单词,如何删除整行

时间:2021-01-22 13:00:13

标签: python pandas

Dataframe 包含名为“product_description”和“manufacturer”的列。我需要删除“product_description”中包含“manufacturer”的所有行。

我试过这个代码:

df[~df.product_description.str.contains(df.manufacturer)]

它给了我错误

TypeError: 'Series' objects are mutable, thus they cannot be hashed

还有其他方法吗?
非常感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

如果需要根据列 manufacturer 的每个值测试列 product_description 中的所有值,请使用 join| 用于正则表达式 or

df[~df.product_description.str.contains('|'.join(data.manufacturer))]

或者如果需要每行测试:

df[df.apply(lambda x: x.product_description not in x.manufacturer, axis=1)]