Replace the values of a given column in pandas with a dictionary
In the dictionary the key is acronym and the value is the state. I assume that State column is not an acronym. You may have to inverse the dictionary such as inv_map = {v: k for k, v in states.items()}
So, I have a dataframe with the name of all the states in the USA and a few cities for each state. However, I need to change the names of the states to its two letter acronyms (ex: Nevada becomes NV and Wyoming becomes WY).
A have a dictionary with the name of the states/territories and its respective acronym, as you can see bellow:
states = {'OH': 'Ohio', 'KY': 'Kentucky', 'AS': 'American Samoa', 'NV': 'Nevada', 'WY': 'Wyoming', 'NA': 'National', 'AL': 'Alabama' and on an on an on
I was thinking about creating a new column, and once it was created, I'd drop the 'States' column. But my code bellow is filling the new column with NaN values
state_town_df['acronym'] = state_town_df['State'].map(states)
Is there any simple way to solve it?