Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How I can replace values with src.replace method in pandas?

I want to replace values in certain column.

example of datatable is below,

Name of datatable is df

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading