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?

Emil
  • 1,531
  • 3
  • 22
  • 47
  • Have a look here - https://stackoverflow.com/questions/13331698/how-to-apply-a-function-to-two-columns-of-pandas-dataframe – Tom Ron Apr 30 '19 at 10:22
  • 1
    Possible duplicate of [How to apply a function to two columns of Pandas dataframe](https://stackoverflow.com/questions/13331698/how-to-apply-a-function-to-two-columns-of-pandas-dataframe) – Tom Ron Apr 30 '19 at 10:23
  • You can use a `lambda` function here: `df['msgText'] = df.apply(lambda x: x['msgText'].replace(x['name'], 'Karl'), axis=1)` – Erfan Apr 30 '19 at 10:25

1 Answers1

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
David Ng
  • 1,618
  • 10
  • 11