How to fill in abbreviation in one column based on state name in another column?

David Shi davidgshi at yahoo.co.uk
Sun May 1 14:27:39 EDT 2016


Hello, I am back.  Thank you very much for your positive response.
I am trying to use Pandas apply to execute a lookup function, so that we can put abbreviation in a new column, in accordance to a state name in another column.
Does anyone knows how to make this to work?
Regards.DavidLook up functionstate_to_code = {"VERMONT": "VT", "GEORGIA": "GA", "IOWA": "IA"}#table['moa_state_name'] = map(lambda x: x.upper(), table['moa_state_name'])def convert_state(row):    abbrev1 =  state_to_code(table['moa_state_name']) #'aatest'    if abbrev1:         return abbrev1 ##state_to_code[abbrev[0]]    return np.nan#print convert_state(table['moa_state_name'])
table.insert(0, "abbrev", np.nan)
table['abbrev'] = table.apply(convert_state, axis=1)print state_to_code['ARKANSAS']



More information about the Python-list mailing list