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


Groups > comp.lang.python > #107951 > 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-05-01 18:27 +0000
Last post2016-05-01 20:31 -0700
Articles 2 — 2 participants

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-05-01 18:27 +0000
    Re: How to fill in abbreviation in one column based on state name in another column? Rustom Mody <rustompmody@gmail.com> - 2016-05-01 20:31 -0700

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

FromDavid Shi <davidgshi@yahoo.co.uk>
Date2016-05-01 18:27 +0000
SubjectHow to fill in abbreviation in one column based on state name in another column?
Message-ID<mailman.290.1462127444.32212.python-list@python.org>
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']

[toc] | [next] | [standalone]


#107964

FromRustom Mody <rustompmody@gmail.com>
Date2016-05-01 20:31 -0700
Message-ID<c5380d68-8377-4866-855c-9f3abb9369c3@googlegroups.com>
In reply to#107951
Your code (below) is too garbled to be able to read

On Monday, May 2, 2016 at 12:00:59 AM UTC+5:30, David Shi wrote:
> 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']

[toc] | [prev] | [standalone]


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


csiph-web