NumPy where chooses between two alternatives at each position of an array
A condition can decide which of two alternatives supplies each output entry. In its three argument form, NumPy where selects from the first alternative when the condition is true and from the second when it is false. Academia Master Trader explains that form in English for readers in Uzbekistan. Constructed examples show how the selection preserves a common output shape instead of simply dropping the positions where a condition is false.
Choose a value at every position
After import numpy as np, define a = np.array([2,5,8,11]). The expression np.where(a < 7,a,0) returns [2,5,0,0]. The condition is [True,True,False,False]. At the first two positions, the output comes from a. At the remaining positions, it comes from the scalar zero.
The result still has four entries. Where has not removed eight and eleven to produce a shorter vector. It has filled their corresponding output positions with the false alternative. The original a retains its values because this call constructs a result rather than assigning that result back into a.

Both alternatives can be arrays
Define b = np.array([20,50,80,110]). The call np.where(a < 7,a,b) returns [2,5,80,110]. At each true position it selects the value from a at that position. At each false position it selects the value from b at that position. It does not take all of a first and append part of b afterward.
Reversing the alternatives reverses their roles. The expression np.where(a < 7,b,a) returns [20,50,8,11]. The condition stays the same, but true now selects from b and false selects from a. State the roles of the second and third arguments when explaining a selection, particularly if both contain plausible output values.
Matrices retain their selection layout
Define m = np.array([[1,4],[7,10]]). The call np.where(m < 6,m,-1) returns [[1,4],[-1,-1]]. The condition and output have shape (2,2). Each output position corresponds to the same row and column position in the source matrix.
This makes where different from a filtering operation that gathers only matching entries into a vector. A matrix shaped result can retain the placement of both matched and unmatched positions. The replacement value minus one is part of the chosen rule. It does not establish that the original entries were negative, absent or incorrectly recorded.
Broadcasting defines the common shape
The condition and both alternatives must be broadcastable to a common shape. A scalar alternative can supply the same value across many positions, as zero and minus one do above. Equal shaped arrays also fit the rule. Inputs that cannot broadcast to a common shape raise an error instead of defining a meaningful position by position selection.
For example, np.where([[True],[False]],[10,20,30],-1) returns [[10,20,30],[-1,-1,-1]]. The two by one condition and the three entry alternative broadcast to shape (2,3). The first condition row selects the vector, while the second selects the scalar. Check this output shape before using a broadcast selection in another calculation.
Keep the one argument form separate
Calling where with only a condition has a different purpose: it returns the nonzero positions as a tuple of index arrays. It does not provide two alternatives or return the selected source values. The documentation recommends using nonzero directly for that purpose. Do not read a one argument call as shorthand for the three argument selection described here.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the condition, true alternative, false alternative and common shape. Then trace the output position by position so the selection rule remains explicit.