Guided Notes

Search, Sort & Filter

Finding values with where and searchsorted, sorting, boolean filtering, and set operations.

Searching with where()

Slide 1 / 5

np.where(condition) returns the indexes where a condition is true:

arr = np.array([1, 2, 3, 4, 5, 4, 4]) np.where(arr == 4) β†’ (array([3, 5, 6]),) np.where(arr % 2 == 0) β†’ indexes of the even values

The result is a tuple with one array per dimension.

With three arguments, np.where chooses values element by element:

np.where(arr > 3, 'high', 'low')

← Reshape, Join & Split