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


Groups > comp.lang.python > #107925

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

From Joel Goldstick <joel.goldstick@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to fill in abbreviation in one column based on state name in another column?
Date 2016-04-30 16:49 -0400
Message-ID <mailman.272.1462049740.32212.python-list@python.org> (permalink)
References <225454564.8325693.1462048831951.JavaMail.yahoo.ref@mail.yahoo.com> <225454564.8325693.1462048831951.JavaMail.yahoo@mail.yahoo.com> <CAPM-O+zaPHooXiH2LZYpDxdL+F-qJMGMcvk38Vu=jweMeuMP=Q@mail.gmail.com>

Show all headers | View raw


could you post in plaintext as its really hard to figure out what your
code is doing

On Sat, Apr 30, 2016 at 4:40 PM, David Shi via Python-list
<python-list@python.org> wrote:
> 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



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to fill in abbreviation in one column based on state name in another column? Joel Goldstick <joel.goldstick@gmail.com> - 2016-04-30 16:49 -0400

csiph-web