Conditional Selection with Numpy

02.01.2021

Conditional Selection with Numpy

One neat feature, which is expanded on in Pandas as well, is conditional selection. In a numpy array, we can use the where method to pass a conditional to select items in an array. In this article, we will look at a quick example of how to select elements conditionally.

arr = np.array([1, 2, 3, 4, 5])

## Select all elements less than 4
np.where(arr < 4)