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

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

我有两列是逗号分隔的单词和单字组合的字符串格式。col1 将始终只包含一个单词。在这个例子中,我将使用单词 语带 col1但这在实际数据中会有差异,所以请不要用regex来解决。 具体来说。

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
3个回答
1
投票

(^,|,$) 处理起始&尾部的逗号。 (,\s|,) 替换操作后,将删除保留的逗号。 {1,} 跳过不重复的逗号

df['col2'] = df['col2'].str. \
    replace("|".join(df['col1'].unique()), "").str.strip() \
    .str.replace("(?:^,|,$)", "") \
    .str.replace("(?:,\s|,){1,}", ",")

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

2
投票

IIUC:

import re
df['col2'] = [(re.sub(fr"({word}[\s,]*)","",sentence)) 
             for word,sentence in zip(df.col1,df.col2)]
df

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

另一个DF,中间有狗。

df = pd.DataFrame({"col1": ["Dog", "Dog", "Dog", "Dog","Dog"],
                     "col2": ["Cat, Mouse", "Dog", "Cat", "Dog, Mouse", "Cat, Dog, Mouse"]})

df


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

应用上面的代码。

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

1
投票

l=df.col1.tolist()#list of col1

创立集从 col2评价成员资格 l 通过应用lambda函数寻找差值,在集合中。

df['col2']=list(zip(df.col2))
df['col2']=df.col2.apply(lambda x:[*{*x}-{*l}]).str[0]

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.