python - Pandas: Change column values based on dictionary keeping rows with no match -


i have data frame country names , modify of values based on dictionary:

endict = {     "republic of korea": "south korea",     "united states of america": "united states",     "united kingdom of great britain , northern ireland": "united kingdom",     "china, hong kong special administrative region": "hong kong" } 

i'm able create 'mask' with:

mask = (energy['country'].isin(endict)) 

however, i'd apply mask on 'country' column when 'true' not modify values not contained in dictionary, i.e. keeping values of rows no match.

any idea?

thanks in advance.

we can use series.replace() method:

energy['country'] = energy['country'].replace(endict, regex=true) 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -