根据列值删除字符串中的单词

问题描述 投票:0回答:1

我有两列,它们是用逗号分隔的单词和字符串格式的单个单词的组合。 Col1始终只有一个字。在此示例中,我将使用单词[[Dog作为col1中的单词,但这在实际数据中会有所不同,因此请不要提出专门在dog上使用正则表达式的解决方案。

df = pd.DataFrame({"col1": ["Dog", "Dog", "Dog", "Dog"], "col2": ["Cat, Mouse", "Dog", "Cat", "Dog, Mouse"]})
我想检查col1中的单词是否出现在col2中的字符串中,如果确实出现,我想从col2中删除该单词。但是请记住,如果还有更多的单词,我想保留字符串的其余部分。因此它将从此开始:

col1 col2 0 Dog Cat, Mouse 1 Dog Dog 2 Dog Cat 3 Dog Dog, Mouse

为此:

col1 col2 0 Dog Cat, Mouse 1 Dog 2 Dog Cat 3 Dog Mouse

python pandas string string-comparison
1个回答
0
投票
IIUC,

df['col2'] = [end.replace(start,"").strip(", ") if start in end else end for start, end in zip(df.col1,df.col2)] col1 col2 0 Dog Cat, Mouse 1 Dog 2 Dog Cat 3 Dog Mouse

您可能必须根据您的特定用例进行调整
© www.soinside.com 2019 - 2024. All rights reserved.