Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

5 Techniques to Search NumPy array

  • Safa MulaniSafa Mulani
  • March 31, 2021
  • NumPy, Python Programming
5 Techniques to Search NumPy array

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:

  1. The argmax() function
  2. The nanargmax() function
  3. The argmin() function
  4. The nargmin() function
  5. 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!! 🙂

Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
NumPy Linear Algebraic functions to know!
Previous Post NumPy Linear Algebraic functions to know!
Next Post 3 Easy Sorting Techniques in NumPy
3 Easy Sorting Techniques in NumPy

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
What Does %s Mean in Python String Formatting?

What Does %s Mean in a Python Format String?

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.