[英]Concatenate two columns excepting strings from a list_pandas
I concatenate two columns for the situation where strings from column 'words' are not present in the column 'sentence'.对于列“单词”中的字符串不存在于“句子”列中的情况,我将两列连接起来。 My code is:
我的代码是:
def check(row):
df['sentence'] = df['sentence'].astype(str)
df['words'] = df['words'].astype(str)
left = row['sentence'].split()
right = row['words'].split()
unmatched = []
for y in left:
word = "".join([x for x in y.lower() if x not in string.punctuation])
if word not in [v.lower() for v in right]:
unmatched.append(y)
return " ".join(unmatched)
mask = df['type'] == 'Is there a match with the Words?'
df.loc[mask, 'new'] = df.loc[mask, :].apply(check, axis=1)
df['new'] = np.where(c, df['new'] + ' ' + df['words'], df['new'])
df['new'] = df['new'].str.replace('nan', '')
df['new'] = df['new'].fillna("")
Additionally, I want to restrict the concatenation per row if, in column 'words' I have strings present in this list:此外,如果在“单词”列中我有此列表中存在的字符串,我想限制每行的串联:
restricted = ['not present', 'for sale', 'unknown']
Here is an example of how the result should look like这是一个结果应该如何的示例
words sentence output
0 unknown This is a new paint This is a new paint
1 brown This is a new item This is a new item brown
2 for sale The product is new The product is new
Output given by the code above is:上面代码给出的 Output 是:
output
This is a new paint unknown
This is a new item brown
The product is new for sale
Given:鉴于:
words sentence
0 unknown This is a new paint
1 brown This is a new item
2 for sale The product is new
Doing:正在做:
restricted = ['not present', 'for sale', 'unknown']
mask = df.words.str.contains('|'.join(restricted))
df['output'] = df.sentence.where(mask, df.sentence + ' ' + df.words)
print(df)
Output: Output:
words sentence output
0 unknown This is a new paint This is a new paint
1 brown This is a new item This is a new item brown
2 for sale The product is new The product is new
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.