Performing Boolean selection
Indexes give us a very powerful and efficient means of looking up values in a Series
based upon their labels. But what if you want to look up entries in a Series
based upon the values?
To handle this scenario pandas provides us with Boolean selection. A Boolean selection applies a logical expression to the values of the Series
and returns a new series of Boolean values representing the result of that expression upon each value. This result can then be used to extract only values where True
was a result.
To demonstrate Boolean selection, let's start with the following Series
and apply the greater than operator to determine values greater than or equal to 3
:

This results in a Series
with matching index labels and the result of the expression as applied to the value of each label. The dtype
of the values is bool
.
This series can then be used to select values from the original series. This selection is performed by passing the Boolean results to the []
operator of the...