title: Inverse lookup in pandas: get ordered lists of row- and column-names
body: <p>Given lists of indices and columns, one can easily pull out the elements of a pandas DataFrame using the <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#the-lookup-method" rel="nofollow noreferrer"><code>lookup()</code></a> method. Is there a way to get the ordered lists of indices and columns from a given data frame (for example, after applying a boolean operation)? To be clear, I want the names of the indices and columns, not just their integer locations.</p>
  
  <p>This is the closest I've come up with, although it's a bit contorted:</p>
  
  <pre><code>In [137]: df = pandas.DataFrame({"a":range(3), "b":range(10,13), "c":range(20,23)}, index=list("ABC"))
  
  In [138]: df
  Out[138]: 
     a   b   c
  A  0  10  20
  B  1  11  21
  C  2  12  22
  
  In [139]: df % 3 == 0
  Out[139]: 
         a      b      c
  A   True  False  False
  B  False  False   True
  C  False   True  False
  
  In [140]: numpy.where(df % 3 == 0)
  Out[140]: (array([0, 1, 2]), array([0, 2, 1]))
  
  In [141]: iindices, icolumns = numpy.where(df % 3 == 0)
  
  In [142]: indices = df.index[iindices]
  
  In [143]: columns = df.columns[icolumns]
  </code></pre>
  
  <p>The result I'm looking for:</p>
  
  <pre><code>In [144]: indices, columns
  Out[144]: 
  (Index([u'A', u'B', u'C'], dtype='object'),
   Index([u'a', u'c', u'b'], dtype='object'))
  </code></pre>
  
  <p>Alternate form that's easier to look at by eye:</p>
  
  <pre><code>In [145]: zip(indices, columns)
  Out[145]: [('A', 'a'), ('B', 'c'), ('C', 'b')]
  </code></pre>
  
  <p>(h/t <a href="https://stackoverflow.com/questions/14016247/python-find-integer-index-of-rows-with-nan-in-pandas?rq=1">Python - find integer index of rows with NaN in pandas</a>)</p>