title: How do I create a function that will return a value in a dictionary for each row within a data sheet using Python?
body: <p>I need to create a new column in my table for a state region that populates a region for every row of data (each having a state). How do I write a function to call upon a dictionary for each row item?</p>
  
  <p>I have about 30,000 row items, and I believe a loop would take too long. I am certain there is some way to do this with dictionaries. I've tried using different methods to call this but cannot seem to get it to populate the correct data.</p>
  
  <pre><code>states = {
          'AK': 'Alaska',
          'AL': 'Alabama',
          'AR': 'Arkansas',
          'AZ': 'Arizona',
          'CA': 'California',
          'CO': 'Colorado',
          'CT': 'Connecticut',
          'DC': 'District of Columbia',
          'DE': 'Delaware',
          'FL': 'Florida',
          'GA': 'Georgia',
          'HI': 'Hawaii',
          'IA': 'Iowa',
          'ID': 'Idaho',
          'IL': 'Illinois',
          'IN': 'Indiana',
          'KS': 'Kansas',
          'KY': 'Kentucky',
          'LA': 'Louisiana',
          'MA': 'Massachusetts',
          'MD': 'Maryland',
          'ME': 'Maine',
          'MI': 'Michigan',
          'MN': 'Minnesota',
          'MO': 'Missouri',
          'MS': 'Mississippi',
          'MT': 'Montana',
          'NC': 'North Carolina',
          'ND': 'North Dakota',
          'NE': 'Nebraska',
          'NH': 'New Hampshire',
          'NJ': 'New Jersey',
          'NM': 'New Mexico',
          'NV': 'Nevada',
          'NY': 'New York',
          'OH': 'Ohio',
          'OK': 'Oklahoma',
          'OR': 'Oregon',
          'PA': 'Pennsylvania',
          'RI': 'Rhode Island',
          'SC': 'South Carolina',
          'SD': 'South Dakota',
          'TN': 'Tennessee',
          'TX': 'Texas',
          'UT': 'Utah',
          'VA': 'Virginia',
          'VT': 'Vermont',
          'WA': 'Washington',
          'WI': 'Wisconsin',
          'WV': 'West Virginia',
          'WY': 'Wyoming'
  }
  
  state_abbrev = {v: k for k, v in states.items()}
  
  state_code = {
      'AK': '10','AL': '4', 'AR': '9', 'AR': '6', 'CA': '9', 'CO': '8',  'CT': '1', 'DC': '3', 'DE': '3', 'FL': '4',
  'GA': '4', 'HI': '9', 'IA': '7', 'ID': '10', 'IL': '5', 'IN': '5', 'KS': '7', 'KY': '4', 'LA': '6', 
  'MA': '1', 'MD': '3', 'ME': '1', 'MI': '5', 'MN': '5','MO': '7', 'MS': '4', 'MT': '8', 'NC': '4', 
  'ND': '8', 'NE': '7', 'NH': '1', 'NJ': '2', 'NM': '6','NV': '9', 'NY': '2', 'OH': '5', 'OK': '6', 
  'OR': '10', 'PA': '3', 'PR': '2', 'RI': '1', 'SC': '4', 'SD': '8', 'TN': '4', 'TX': '6', 'UT': '8',
  'VA': '3', 'VI': '2', 'VT': '1', 'WA': '10', 'WI': '5', 'WV': '3', 'WY': '8', 'PI': '9'
      }
  
  state_region = {v: k for k, v in state_code.items()}
  </code></pre>
  
  <p>def get_region():
      return [state_region[i] for i in fulldf['state']]</p>
  
  <p>fulldf["Region"] = get_region()
  fulldf.tail()</p>
  
  <p>Returns key error 'MA', expected to return a new column named "Region" that populates the region for each "state" listed.</p>
  
  <pre><code>KeyError                                  Traceback (most recent call last)
  &lt;ipython-input-338-6afc1e48556a&gt; in &lt;module&gt;
   33     return [state_region[i] for i in fulldf['state']]
   34 
  ---&gt; 35 fulldf["Region"] = get_region()
   36 fulldf.tail()
   37 
  
  &lt;ipython-input-338-6afc1e48556a&gt; in get_region()
   31 
   32 def get_region():
  ---&gt; 33     return [state_region[i] for i in fulldf['state']]
   34 
   35 fulldf["Region"] = get_region()
  
  &lt;ipython-input-338-6afc1e48556a&gt; in &lt;listcomp&gt;(.0)
   31 
   32 def get_region():
  ---&gt; 33     return [state_region[i] for i in fulldf['state']]
   34 
   35 fulldf["Region"] = get_region()
  
  KeyError: 'MA'
  </code></pre>