I have a dataframe with a column in it containing state names. The names are a mix of official abbreviations and partial spellings and complete state names.
d = pd.DataFrame(['fla', 'fl', 'del', 'ohio', 'calif', 'ca', 'del', 'texas', 'miss', 'tx', 'new mex'],
columns = ["state"])
There is a python dict with state abbrevs and names here: https://code.activestate.com/recipes/577305-python-dictionary-of-us-states-and-territories/
I would like to look in the dataframe d and find the best match in the dict and substitute for the values in d['state']. I don't think I want to use replace because i want to replace the "whole word" rather than the substring. The desired result:
d = ['fl', 'fl', 'de', 'oh', 'ca', 'ca', 'de', 'tx', 'ms', 'tx', 'nm']
Loading the dict directly into my console, and calling it states_dict, I tried the following (based on this map US state name to two letter acronyms that was given in dictionary separately)
d['state'] = d['state'].map(states_dict)
which produced nan for every entry in my dataframe, d.
Any help would be much appreciated.
Thanks.