Home > Back-end >  replace values in a pandas df column if they appear in another column
replace values in a pandas df column if they appear in another column

Time:07-09

I have a pandas dataframe and would like to replace words in column something if they are the same as column to_hide. So I'd like to go from here

    to_hide                something
0       bla  there is bla over there
1       sth    i cannot see anything
2  sth else    sth else is beautiful

to here

  to_hide                    something
0       bla  there is to_hide over there
1       sth        i cannot see anything
2  sth else         to_hide is beautiful

The below unfortunately throws an error

df = pd.DataFrame({'to_hide':['bla','sth','sth else'], 'something':['there is bla over there','i cannot see anything','sth else is beautiful']})
df.assign(**{'something' : df['something'].str.replace(df['to_hide'], 'to_hide')})
TypeError: 'Series' objects are mutable, thus they cannot be hashed

how can I write it so it works?

CodePudding user response:

You need to use a loop here:

df['something'] = [s.replace(pat, 'to_hide')
                   for pat, s in zip(df['to_hide'], df['something'])]

output (as new column for clarity):

    to_hide                something                   something2
0       bla  there is bla over there  there is to_hide over there
1       sth    i cannot see anything        i cannot see anything
2  sth else    sth else is beautiful         to_hide is beautiful

CodePudding user response:

Alternative would be to go with apply():

df['something'] = df.apply(lambda x: x.something.replace(x.to_hide, 'to_hide'), axis=1)
Example
import pandas as pd

df = pd.DataFrame({'to_hide':['bla','sth','sth else'], 'something':['there is bla over there','i cannot see anything','sth else is beautiful']})
df['something'] = df.apply(lambda x: x.something.replace(x.to_hide, 'to_hide'), axis=1)
Output
to_hide something
0 bla there is to_hide over there
1 sth i cannot see anything
2 sth else to_hide is beautiful
  • Related