Numpy select

Author

Simone Massaro

Published

March 6, 2026

You can use numpy.select when you have multiple conditions to apply to an array. It works like a vectorized if/elif/else, which is very convenient.

Small example:

import numpy as np

x = np.array([2, 7, 12, 20, 30])

conditions = [x < 5, x < 15, x <= 20]
choices = ["small", "medium", "large"]

labels = np.select(conditions, choices, default="a real giant")
labels
array(['small', 'medium', 'medium', 'large', 'a real giant'], dtype='<U12')