Free lab · runs in your browser · no sign-up
NumPy array filtering with boolean masks
Filtering is the first thing you do with real data: keep the rows you need. In NumPy you do it without loops, with a boolean mask. Edit the code and press Run.
Step 1: a mask is an array of True and False
Compare an array with a value and you get one True or False per element. Use it inside [ ] to keep only the True ones.
Step 2: combine conditions
Use & (and), | (or) and ~ (not), with brackets around each condition.
Step 3: np.where and filtering a table
np.where picks values by a condition. Filtering one column decides the rows of another.