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']
--
https://mail.python.org/mailman/listinfo/python-list