access pandas axes by axis number
<p>consider the <code>pd.Panel</code> <code>pn</code> and <code>pd.DataFrame</code> <code>df</code></p>

<pre><code>import pandas as pd
import numpy as np

pn = pd.Panel(np.arange(27).reshape(3, 3, 3), list('XYZ'), list('abc'), list('ABC'))

pn.to_frame().rename_axis('item', 1).unstack()
</code></pre>

<p><a href="https://i.stack.imgur.com/3Gpon.png" rel="nofollow"><img src="https://i.stack.imgur.com/3Gpon.png" alt="enter image description here"></a></p>

<pre><code>df = pd.DataFrame(np.arange(9).reshape(3, 3), list('abc'), list('ABC'))

df
</code></pre>

<p><a href="https://i.stack.imgur.com/gyjvS.png" rel="nofollow"><img src="https://i.stack.imgur.com/gyjvS.png" alt="enter image description here"></a></p>

<hr>

<p>I can access the <code>items</code> axis of <code>pn</code> with <code>pn.items</code> and the <code>columns</code> axis of <code>df</code> with <code>df.columns</code>.</p>

<p><strong><em>Question</em></strong><br>
But how do I get the <code>items</code> axis of <code>pn</code> with the number <code>0</code> and <code>minor_axis</code> axis with the number <code>2</code>?  With as many methods that accept the parameter <code>axis=0</code>, I'm imagining there is a straight forward way to access axes via number.</p>

<p><strong><em>What I've done</em></strong><br>
Custom function</p>

<pre><code>def get_axis(obj, axis=0):
    if isinstance(obj, pd.Panel):
        m = pd.Series(['items', 'major_axis', 'minor_axis'])
    else:
        m = pd.Series(['index', 'columns'])
    return obj.__getattribute__(m.loc[axis])

print(get_axis(pn, 2))
print(get_axis(pn, 1))
print(get_axis(pn, 0))
print(get_axis(df, 1))
print(get_axis(df, 0))
</code></pre>

<hr>

<pre><code>Index([u'A', u'B', u'C'], dtype='object')
Index([u'a', u'b', u'c'], dtype='object')
Index([u'X', u'Y', u'Z'], dtype='object')
Index([u'A', u'B', u'C'], dtype='object')
Index([u'a', u'b', u'c'], dtype='object')
</code></pre>

<p>Use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Panel.axes.html" rel="nofollow"><code>.axes</code></a> to return a list of the index axis labels as well as the column axis labels and then you could access it via slice-notation.</p>

<pre><code>pn.axes
#[Index(['X', 'Y', 'Z'], dtype='object'),
# Index(['a', 'b', 'c'], dtype='object'),
# Index(['A', 'B', 'C'], dtype='object')]
</code></pre>

<p>Then, you could provide the slices to retrieve the objects:</p>

<pre><code>pn.axes[0]
#Index(['X', 'Y', 'Z'], dtype='object')

df.axes[0]
#Index(['a', 'b', 'c'], dtype='object')
</code></pre>

python pandas