我有两列,它们是用逗号分隔的单词和字符串格式的单个单词的组合。 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
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
您可能必须根据您的特定用例进行调整