-1

I have a panda data frame "df" with a column having enteries like NY, DM.....etc and many thousands of enteries , i am given a dictionary {'US' : 'United States', 'DM':'DENMARK'....etc} to replace all these short form names according to given dictionary, how to do that?

AMC
  • 2,642
  • 7
  • 13
  • 35
wm 50
  • 27
  • 6
  • What is the issue, exactly? Have you tried anything, done any research? – AMC Apr 14 '20 at 18:37
  • 2
    Does this answer your question? [Remap values in pandas column with a dict](https://stackoverflow.com/questions/20250771/remap-values-in-pandas-column-with-a-dict) – AMC Apr 14 '20 at 18:38

1 Answers1

1

Try the following:

#Create a one-time dictionary with mapping of country against full name

d = {'US':'United States','DM':'DENMARK'}

#And use pandas map function to create a new column

df['Country_Full'] = df['Country'].map(d)

print(df)
  Country Country_Full
0      US   United States
1      DN   DENMARK

If you want to replace content from the existing column:

df['Country'] = df['Country'].map(d)
ManojK
  • 1,570
  • 2
  • 9
  • 17
  • 1
    Also may be you can use [`pycountry`](https://pypi.org/project/pycountry/) using which you can create a dictionary of countries abbreviations and names `import pycountry` ; `d = dict([(i.alpha_2,i.name) for i in pycountry.countries])` , gives `{'AW': 'Aruba', 'AF': 'Afghanistan', 'AO': 'Angola', ...}` – anky Apr 14 '20 at 17:54
  • 1
    @anky - Something new to me, thanks for sharing!! – ManojK Apr 14 '20 at 18:05