## Introduction to numPy.where()

The numPy.where() function is used to deliver back to the user the specific indices of certain elements which are present in the array which has been entered by the user where certain predefined conditions with respect to the function parameters get satisfied. In simple words, we can say that the function helps the user to locate where exactly is the element with the specified conditions positioned in the array which has been entered. This can be done by using both single argument as well as specifying multiple arguments.

**Syntax for numPy.where() function**

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

**Parameters for numPy.where() function in Python language**

`condition : array_like , bool`

The conditional check to identify the elements in the array entered by the user complies with the conditions that have been specified in the code syntax. If the condition is false to be TRUE, the value x is used. If the condition false is to be FALSE, then the value y is rather used. These are generally Boolean values.

`x, y: array_like`

The values which are to be replaced in case of a true or false condition and the conditions as the parameter has to be converted into a broadcasted to structured array for the resultant elements to be displayed which needed to have a specific orientation.

**Returns for numPy.where() function in Python language**

`out:ndarray`

It is the output array that is assimilated after the original array entered by the user is processed for the conditions applied, and the elements are replaced by the conditional elements X and Y.

**Note while the conditional parameters for numPy.where() function is passed**

- It must be noted that when all the arguments in your conditions have been passed and the resultant are produced with the elimination of the conditional values replaced by the selected X and Y values would be produced, all free of the arrays should be of similar dimensions of size.
- If the replacement values (i.e., x and y arguments) have not been passed and the condition alone has been applied to the numpy.where() function then the resultant reproduce would be array tuples (i.e., one array for each of the axis of the original array entered). These array tuples would contain the specific indices the elements which test out to be matching the true value for the Boolean condition which has been passed.

### How does the numPy.where() Function work?

In functional ability of the numPy.where() we have to see that when the logical operation is being run through the function a Boolean array is returned as a result off the condition being true.

Visually display what the system processes and identifies while running the numpy.where() function:

Create an array containing a list of elements:

`a1 = n1.array([21 , 22 , 23, 24 , 25 , 26 , 27 , 28 , 29 , 30)]`

Passing a conditional expression for the array entered to be tested:

`ans = n1.where((a1 > 22) & (a1 < 26))`

The function runs through each element in the array entered by the user to check if it performs with the condition given in the function. For each element which test to be true, to the numpy.where() captures the indices of the element into a new array containing the indices of each of the element testing true.

This diagram shows the mechanism of how the array is checked for the specific condition implied and returns the output returning the relevant output array in accordance with indices returned.

### Examples of numPy.where() Function

The following example displays how the numPy.where() function is used in a python language code to conditionally derive out elements complying with conditions:

#### Example #1

Python numPy function integrated program which illustrates the use of the where() function.

**Code:**

```
import numpy as n1
# entering the array
a1 = n1.array([[10, 20, 30], [40, 50, 60]])
# printing the elements in the given array
print ("The original array entered by the user")
print(a1)
# specifically sorting out the array which are greater than 30
print ("Print all indices containing elements greater than 30")
ans = n1.where(a1>30)
print(ans)
print("Displaying all the elements of the array that are greater than 30")
print(a1[ans])
```

**Output:**

#### Example #2

Python numPy function integrated program which illustrates the use of the where() function.

**Code:**

```
import numpy as n2
# entering the elements of the original array
a2 = n2.array([[4, 5, 6], [7, 8, 9]])
# printing the elements in the given array
print ("The original array entered by the user")
print(a2)
# specifically sorting out the array which are less than 7
print ("Print all indices containing elements less than 7 ")
ans2 = n2.where(a2<7)
print(ans2)
print("Displaying all the elements of the array that are less than 7")
print(a2[ans2])
```

**Output: **

### Conclusion

The numPy.where() function provides for a pre-developed functionality which is quintessential and very handy acting as a preparatory step functions for major large threshold data processing programs. It serves as a solution against the creation of many additional arrays that stores new elements specifically which increased the processing time and reduces the verbosity of the program.

### Recommended Articles

This is a guide to numPy.where(). Here we discuss the introduction to numPy.where() along with how does the function work and respective examples. You may also have a look at the following articles to learn more –

4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses