Hello, readers! In this article, we will be focusing on 5 techniques to Search NumPy arrays with conditions, in detail.

So, let us begin! 🙂

A NumPy array stores the elements of similar types in a continuous structure. We often come across situations where we need to look at the maximum and minimum elements of the arrays at dynamic run-time. NumPy offers us a set of functions that enables us to search for specific elements having certain conditions applied to them.

## How to search NumPy arrays for specific elements?

Let’s see the 5 functions used to search NumPy arrays in detail:

**The argmax() function****The nanargmax() function****The argmin() function****The nargmin() function****Searching using where() function**

### 1. NumPy argmax() function

With the NumPy **argmax() function**, we can easily fetch and display the index of the maximum (largest) element present in the array structure.

By this, the index of the largest elements is resultant value from the argmax() function.

**Syntax:**

```
numpy.argmax() function
```

**Example:**

```
import numpy as np
data = np.array([[66, 99, 22,11,-1,0,10],[1,2,3,4,5,0,-1]])
res = np.argmax(data)
print(data)
print("Max element's index:", res)
```

**Output:**

In the above example, we have created two arrays of same data type. Further, have applied argmax() function to get the index of the max element from all the elements. As 99 is the largest element, 1 is displayed as the resultant index value.

```
[[66 99 22 11 -1 0 10]
[ 1 2 3 4 5 0 -1]]
Max element's index: 1
```

### 2. NumPy nanargmax() function

With **nanargmax() function**, we can easily deal with the NAN or NULL values present in the array. That is, it does not get treated differently. The NAN values then have no effect on the functioning of the search values.

**Syntax:**

```
numpy.nanargmax()
```

**Example:**

In the below example, the array elements contain a NULL value passed using numpy.nan function. Further, we now use nanargmax() function to search NumPy arrays and find the maximum value from the array elements without letting the NAN element affecting the search.

```
import numpy as np
data = np.array([[66, 99, 22,np.nan,-1,0,10],[1,2,3,4,np.nan,0,-1]])
res = np.nanargmax(data)
print(data)
print("Max element's index:", res)
```

**Output:**

```
[[66. 99. 22. nan -1. 0. 10.]
[ 1. 2. 3. 4. nan 0. -1.]]
Max element's index: 1
```

### 3. NumPy argmin() function

With** argmin() function**, we can search NumPy arrays and fetch the index of the smallest elements present in the array at a broader scale. It searches for the smallest value present in the array structure and returns the index of the same. Thus, with the index, we can easily get the smallest element present in the array.

**Syntax:**

```
numpy.argmin() function
```

**Example:**

```
import numpy as np
data = np.array([[66, 99, 22,11,-1,0,10],[1,2,3,4,5,0,-1]])
res = np.argmin(data)
print(data)
print("Min element's index:", res)
```

**Output:**

As seen below, there are two indexes that occupies the lowest element i.e. [-1]. But, the argmin() function returns the index of the first occurrence of the smallest element from the array values.

```
[[66 99 22 11 -1 0 10]
[ 1 2 3 4 5 0 -1]]
Min element's index: 4
```

### 4. NumPy where() function

With **where() function**, we can easily search NumPy arrays for the index values of any element which matches the condition passed as a parameter to the function.

**Syntax:**

```
numpy.where(condition)
```

**Example:**

```
import numpy as np
data = np.array([[66, 99, 22,11,-1,0,10],[1,2,3,4,5,0,-1]])
res = np.where(data == 2)
print(data)
print("Searched element's index:", res)
```

**Output:**

In this example, we have searched for an element from the array whose value is equal to 2. Further to which, the where() function returns the array index and the data type of the same.

```
[[66 99 22 11 -1 0 10]
[ 1 2 3 4 5 0 -1]]
Searched element's index: (array([1], dtype=int64))
```

### 5. NumPy nanargmin() function

With **nanargmin() function**, we can easily search NumPy arrays to find the index of the smallest value present in the array elements without having to worry about the NAN values present in them. The NULL values have zero effect on the search of the elements.

**Syntax:**

```
numpy.nanargmin()
```

**Example:**

```
import numpy as np
data = np.array([[66, 99, np.nan,11,-1,0,10],[1,2,3,4,5,0,-1]])
res = np.nanargmin(data)
print(data)
print("Searched element's index:", res)
```

**Output:**

```
[[66. 99. nan 11. -1. 0. 10.]
[ 1. 2. 3. 4. 5. 0. -1.]]
Searched element's index: 4
```

## Conclusion

Feel free to comment below in case you come across any question. For more such posts related to Python programming, stay tuned with us.

Till then, Happy learning!! 🙂