我有以下格式的数据集:
msgText name
'My name is Donald' Donald
'I am married to Jenny' Donald
'Donald is from Europe' Donald
我想做的是替换msgText
列的某些部分,因为它包含了name
列的名称,我想用'Karl'替换它。这样我想要的输出如下所示:
msgText name
'My name is Karl' Donald
'I am married to Jenny' Donald
'Karl is from Europe' Donald
为此,我具有以下功能:
def replaceName(text, name):
newText = text.replace(name, 'Karl')
return newText
但是,我不知道如何将此功能应用于Pandas系列。
我的开始是:
dataset['filtered_text'] = dataset.msgText.apply(replaceName)
但是,这里我不考虑coluimn这个名字。如何使用apply函数并将两列用作函数的输入变量?
答案 0 :(得分:1)
这是您正在寻找的解决方案:
df['msgText'] = df.apply(lambda row: replaceName(row['msgText'], row['name']), axis=1)
print(df)
msgText name
0 'My name is Karl' Donald
1 'I am married to Jenny' Donald
2 'Karl is from Europe' Donald