title: How to handle KeyError: "['blah'] not in index"
body: <p>I'm looking at the US names dataset (SSA), as described in Python for Data Analysis by Wes McKinney.</p>
  
  <p>This <strong>works</strong>:</p>
  
  <pre><code>total_births = top1000.pivot_table('births', index = 'year', columns = 'name', aggfunc = sum)
  subset = total_births[['Michael', 'Mike', 'Martin']].fillna(0)
  subset.plot( title = 'Number of births per year', grid = True, figsize=(28,20), xticks=range(1880, 2020, 5)).get_figure().savefig('output2.png', bbox_inches = 'tight')
  </code></pre>
  
  <p>But when I add an unpopular name, which nevertheless is in the data set:</p>
  
  <pre><code>subset = total_births[['Michael', 'Mike', 'Martin', 'Ammar']].fillna(0)
  </code></pre>
  
  <p>...I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "names.py", line 44, in &lt;module&gt;
      subset = total_births[['Michael', 'Mike', 'Martin', 'Ammar']].fillna(0)
    File "/home/mike/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 1774, in __getitem__
      return self._getitem_array(key)
    File "/home/mike/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 1818, in _getitem_array
      indexer = self.ix._convert_to_indexer(key, axis=1)
    File "/home/mike/anaconda/lib/python2.7/site-packages/pandas/core/indexing.py", line 1143, in _convert_to_indexer
      raise KeyError('%s not in index' % objarr[mask])
  KeyError: "['Ammar'] not in index"
  </code></pre>
  
  <p>I tried adding fillna(0), but it doesn't help... The code is available at <a href="https://github.com/m1key/data-science-sandbox" rel="nofollow">https://github.com/m1key/data-science-sandbox</a> (ade55154f177410e1e269d64766a4e8b8e1ae585), the troublesome lines are commented out.</p>
  
  <p>Sample data set:</p>
  
  <pre><code>name  Aaden  Aaliyah  Aanya  Aarav  Aaron  Aarush  Ab  Abagail  Abb  Abbey  \
  year                                                                         
  1880    NaN      NaN    NaN    NaN    102     NaN NaN      NaN  NaN    NaN   
  1881    NaN      NaN    NaN    NaN     94     NaN NaN      NaN  NaN    NaN   
  1882    NaN      NaN    NaN    NaN     85     NaN NaN      NaN  NaN    NaN   
  1883    NaN      NaN    NaN    NaN    105     NaN NaN      NaN  NaN    NaN   
  1884    NaN      NaN    NaN    NaN     97     NaN NaN      NaN  NaN    NaN   
  
  name  ...   Zoa  Zoe  Zoey  Zoie  Zola  Zollie  Zona  Zora  Zula  Zuri  
  year  ...                                                               
  1880  ...     8   23   NaN   NaN     7     NaN     8    28    27   NaN  
  1881  ...   NaN   22   NaN   NaN    10     NaN     9    21    27   NaN  
  1882  ...     8   25   NaN   NaN     9     NaN    17    32    21   NaN  
  1883  ...   NaN   23   NaN   NaN    10     NaN    11    35    25   NaN  
  1884  ...    13   31   NaN   NaN    14       6     8    58    27   NaN  
  </code></pre>
  
  <p>Thanks for any hints.</p>