Replace part of a string with a pandas column as pattern


I would like to use the .str.replace() method on a pandas column, but with another pandas column as pattern

For example,

df = pd.DataFrame({'str1': ['abc','abcd','def'], 'str2': ['b','ab', 'ef']})

I want to build a new column str1_replaced by replacing in str1 the str2 strings by an empty string.

Here’s the result I want to get:

   str1 str2 str1_replaced
0   abc    b            ac
1  abcd   ab            cd
2   def   ef             d

I’ve tried to do:

df['str1_replaced'] = df['str1'].str.replace(df['str2'],"")

But I get the following error

TypeError: ‘Series’ objects are mutable, thus they cannot be hashed

Is there any way to achieve that without using a for loop? I’m thinking of using a lambda function but cannot find out exactly how to do it.


Try apply:

df['str1_replaced'] = df.apply(lambda x: x['str1'].replace(x['str2'], ''), axis=1)

>>> df
   str1 str2 str1_replaced
0   abc    b            ac
1  abcd   ab            cd
2   def   ef             d

Or try list comprehension:

df['str1_replaced'] =[x.replace(y, '') for x, y in zip(df['str1'], df['str2'])]

Answered By – U12-Forward

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published