Home > Net >  Replace part of a string with a pandas column as pattern
Replace part of a string with a pandas column as pattern

Time:09-24

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.

CodePudding user response:

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'])]
  • Related