Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107924 > unrolled thread

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

Started byDavid Shi <davidgshi@yahoo.co.uk>
First post2016-04-30 20:40 +0000
Last post2016-04-30 20:40 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  How to fill in abbreviation in one column based on state name in another column? David Shi <davidgshi@yahoo.co.uk> - 2016-04-30 20:40 +0000

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

FromDavid Shi <davidgshi@yahoo.co.uk>
Date2016-04-30 20:40 +0000
SubjectHow to fill in abbreviation in one column based on state name in another column?
Message-ID<mailman.271.1462049007.32212.python-list@python.org>
I am trying to use 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.
David
state_to_code = {"VERMONT": "VT", "GEORGIA": "GA", "IOWA": "IA", "Armed Forces Pacific": "AP", "GUAM": "GU",                 "KANSAS": "KS", "FLORIDA": "FL", "AMERICAN SAMOA": "AS", "NORTH CAROLINA": "NC", "HAWAII": "HI",                 "NEW YORK": "NY", "CALIFORNIA": "CA", "ALABAMA": "AL", "IDAHO": "ID", "FEDERATED STATES OF MICRONESIA": "FM",                 "Armed Forces Americas": "AA", "DELAWARE": "DE", "ALASKA": "AK", "ILLINOIS": "IL",                 "Armed Forces Africa": "AE", "SOUTH DAKOTA": "SD", "CONNECTICUT": "CT", "MONTANA": "MT", "MASSACHUSETTS": "MA",                 "PUERTO RICO": "PR", "Armed Forces Canada": "AE", "NEW HAMPSHIRE": "NH", "MARYLAND": "MD", "NEW MEXICO": "NM",                 "MISSISSIPPI": "MS", "TENNESSEE": "TN", "PALAU": "PW", "COLORADO": "CO", "Armed Forces Middle East": "AE",                 "NEW JERSEY": "NJ", "UTAH": "UT", "MICHIGAN": "MI", "WEST VIRGINIA": "WV", "WASHINGTON": "WA",                 "MINNESOTA": "MN", "OREGON": "OR", "VIRGINIA": "VA", "VIRGIN ISLANDS": "VI", "MARSHALL ISLANDS": "MH",                 "WYOMING": "WY", "OHIO": "OH", "SOUTH CAROLINA": "SC", "INDIANA": "IN", "NEVADA": "NV", "LOUISIANA": "LA",                 "NORTHERN MARIANA ISLANDS": "MP", "NEBRASKA": "NE", "ARIZONA": "AZ", "WISCONSIN": "WI", "NORTH DAKOTA": "ND",                 "Armed Forces Europe": "AE", "PENNSYLVANIA": "PA", "OKLAHOMA": "OK", "KENTUCKY": "KY", "RHODE ISLAND": "RI",                 "DISTRICT OF COLUMBIA": "DC", "ARKANSAS": "AR", "MISSOURI": "MO", "TEXAS": "TX", "MAINE": "ME"}
#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']

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web