0

I have a dataset of the following format:

msgText                   name
'My name is Donald'       Donald
'I am married to Jenny'   Donald 
'Donald is from Europe'   Donald

What I would like to do is replace parts of the msgText column fs it contains the name from the name column and I would like to replace it by 'Karl'. So that my desired output looks as follows:

msgText                   name
'My name is Karl'         Donald
'I am married to Jenny'   Donald 
'Karl is from Europe'     Donald

To do so, I have the following function:

def replaceName(text, name):
    newText = text.replace(name, 'Karl')
    return newText

However, I don't know how to apply this function to a Pandas series.

What I started with is:

dataset['filtered_text'] = dataset.msgText.apply(replaceName)

However, here I don't take the name coluimn into consideration. How can I use the apply function and use two columns as input variables to my function?

3

1 Answer 1

1

Here is the solution you are looking for:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.