First page Back Continue Last page Overview Graphics

numpy.where

Syntax:

numpy.where(condition[, x, y])

If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere:

>>> x = 3

>>> np.where(x > 0, 42, 41)

array(42)

>>>

>>>

>>> np.where([[True, False, True], [True, True, False]],

... [[12, 24, 66], [22, 24, 26]],

... [[11,13,15], [23, 25, 27]])

array([[12, 13, 66],

[22, 24, 27]])

>>> a = np.array([3, 12, 14, 9])

>>> b = np.array([11, 13, 15, 19])

>>> c = np.array([23,25,27,29])

>>>

>>>

>>> np.where(a < 10, b, c)

array([11, 25, 27, 19]) >>>