Boolean indexing

Posted on June 30, 2021



Use boolean indexing in NumPy arrays to assign values. The example below assigns a zero to array items that are less than zero.

>>> import numpy as np
>>> nums = np.array([-1, 3, 6, -0.2, -4])
>>> nums[nums < 0] = 0
>>> nums
array([0., 3., 6., 0., 0.])


Back


Gavin Wiggins © 2021