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?