簡體   English   中英

如果一列的字符串包含 pandas dataframe 中另一列的單詞,如何刪除整行

[英]How to drop entire row if string of one column contains the word from another column in pandas dataframe

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

還有其他方法嗎?
非常感謝您的幫助!

如果需要根據列product_description的每個值測試列manufacturer的所有值,請使用join with | 對於正則表達式or

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

或者如果需要每行測試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM