Pandas dataframe.loc : what does "Boolean list with the same length as the row axis" mean?

Viewed 464

Pandas documentation has this Boolean list with the same length as the row axis example, like so:

>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
...      index=['cobra', 'viper', 'sidewinder'],
...      columns=['max_speed', 'shield'])
>>> df
            max_speed  shield
cobra               1       2
viper               4       5
sidewinder          7       8


>>> df.loc[[False, False, True]]
            max_speed  shield
sidewinder          7       8

Can someone please explain how the df.loc[[False, False, True]] works to get the result shown please?

Thanks.

1 Answers

df[[False,False,True]] returns the same as df.loc[[False, False, True]].

df[[False,False,True]] is more intuitive & can be interpreted as: don't return first & second (ie first & second False), return third (ie third is True) row.

In the documentiation, see the bullet point:

  • A boolean array of the same length as the axis being sliced, e.g. [True, False, True].
Related