Home > Back-end >  How I can replace values with src.replace method in pandas?
How I can replace values with src.replace method in pandas?

Time:11-18

I want to replace values in certain column.

example of datatable is below,

Name of datatable is df

column1  column2
aaaa     cup
bbbb     coffee
cccc     juice
dddd     tea

What I want to this result below

column1  column2
aaaa     pink 
bbbb     brown 
cccc     orange
dddd     white

So I tried this below

df['column2'] = df['column2'].str.replace('cup', 'pink')
df['column2'] = df['column2'].str.replace('coffee', 'brown')
df['column2'] = df['column2'].str.replace('juice', 'orange')
df['column2'] = df['column2'].str.replace('tea', 'white')

I got the result with this code, but I think that it's so messy code

So I tried this,


 change_word = {
     'cup':'pink'    ,'coffee':'brown',
     'juice':'orange','tea':'white'
 }
df['column2'].str.replace(change_word, inplace=True)

but it doesn't work. Doesn't str.replace method have a function that converts all at once?

I tried .replace method but for the .replace method, the entire character must match. So it comes out a little different from the result I want.

Is there any idea?

CodePudding user response:

We can try using str.replace with a callback function:

change_word = {
    'cup':'pink'    ,'coffee':'brown',
    'juice':'orange','tea':'white'
}
regex = r'\b(?:'   r'|'.join(change_word.keys())   r')\b'
df["column2"] = df["column2"].str.replace(regex, lambda m: change_word[m.group()], regex=True)

If you are certain that every value in the second column would appear in the dictionary, then you could also use this simplified version:

df["column2"] = df["column2"].str.replace(r'\w ', lambda m: change_word[m.group()], regex=True)
  • Related