Boolean masks

A boolean mask is an array of True/False with the same shape as the data, obtained from a comparison. Used as an index, it selects the elements for which the value is true. Masks are combined with &, | and ~, with each condition in parentheses.

Example

a = np.array([5, 12, 8])
a[a > 6]  # array([12, 8])

Related concepts

The whole glossary