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
Post a Comment