Why does pandas dataframe indexing change axis depending on index type?
<p>when you index into a pandas dataframe using a list of ints, it returns columns.</p>

<p>e.g. <code>df[[0, 1, 2]]</code>  returns the first three columns.</p>

<p>why does indexing with a boolean vector return a list of rows?</p>

<p>e.g. <code>df[[True, False, True]]</code> returns the first and third rows. (and errors out if there aren't 3 rows.)</p>

<p>why? Shouldn't it return the first and third columns?</p>

<p>Thanks!</p>

<p>Because if use:</p>

<pre><code>df[[True, False, True]]
</code></pre>

<p>it is called <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a> by mask:</p>

<pre><code>[True, False, True]
</code></pre>

<p>Sample:</p>

<pre><code>df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9]})

print (df)
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

print (df[[True, False, True]])
   A  B  C
0  1  4  7
2  3  6  9
</code></pre>

<p>Boolean mask is same as:</p>

<pre><code>print (df.B != 5)
0     True
1    False
2     True
Name: B, dtype: bool

print (df[df.B != 5])
   A  B  C
0  1  4  7
2  3  6  9
</code></pre>

python pandas