title: Quick Crosswalk For State Abbreviations & State Names
body: <p>For the millionth time I had a dataset today that listed full state names. But, I needed it to list state postal code abbreviations. Here is a code snip I wrote that mapped the changes for me using data from a generic website. </p>
  
  <p>1) Anyone know of or think of a better solution?</p>
  
  <p>2a) Anyone know of a better web reference? Using USPS sites (such as the ones below) will not seem to work with <code>pd.read_html()</code></p>
  
  <p>2b) I also had a hard time isolating the correct table from <code>pd.read_html()</code> and the wiki page at: <a href="https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations</a></p>
  
  <pre class="lang-py prettyprint-override"><code>import pandas as pd
  
  
  # Make Generic Data For Demonstration Purpose
  data = {'StName':['Wisconsin','Minnesota','Minnesota',
                       'Wisconsin','Florida','New York']}
  df = pd.DataFrame(data)
  
  # Get State Crosswalk From Generic Website
  crosswalk = 'http://app02.clerk.org/menu/ccis/Help/CCIS%20Codes/state_codes.html'
  states = pd.read_html(crosswalk)[0]
  
  # Demo Crosswalking State Name to State Abbreviation
  df['StAbbr'] = df['StName'].map(dict(zip(states['Description'], 
                                           states['Code'])))
  
  # Demo Reverse Crosswalking Back to State Name
  df['StNameAgain'] = df['StName'].map(dict(zip(states['Code'], 
                                                states['Description'])))
  </code></pre>