0

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?

now
  • 15
  • 3
  • What you want is the `map` function: `df['column2'] = df['column2'].map( change_word )`. It does a lookup through a dictionary. https://stackoverflow.com/questions/20250771/remap-values-in-pandas-column-with-a-dict-preserve-nans – Tim Roberts Nov 18 '22 at 07:05

1 Answers1

1

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)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360