NumPy Array Search – Find Values Fast with NumPy Functions
Introduction – Why Learn Array Search in NumPy?
Searching arrays is a critical operation in data analysis and machine learning workflows. Whether you’re locating specific values, checking for conditions, or filtering large datasets, NumPy’s search functions like where(), searchsorted(), and nonzero() help you identify data efficiently and precisely.
In this guide, you’ll learn:
- How to use
np.where()for conditional search - How
np.searchsorted()finds insert positions in sorted arrays - How
np.nonzero()locates all non-zero values - When to use
isin()for membership tests - Real-world use cases for each method
Using np.where() – Condition-Based Search
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
result = np.where(arr > 30)
print(result)
Output:
(array([3, 4]),)
where() returns indices where the condition is True
Can be used for both search and value replacement
Conditional Replacement with where()
arr = np.array([10, 20, 30, 40, 50])
new_arr = np.where(arr > 30, 1, 0)
print(new_arr)
Output:
[0 0 0 1 1]
All elements > 30 replaced with 1, others with 0
Using np.searchsorted() – Search in Sorted Arrays
arr = np.array([10, 20, 30, 40])
pos = np.searchsorted(arr, 25)
print(pos)
Output:
1
Tells you where to insert 25 to keep array sorted
Use side='right' to insert after duplicates
Batch Insert Positions
arr = np.array([10, 20, 30, 40])
values = [5, 15, 35]
positions = np.searchsorted(arr, values)
print(positions)
Output:
[0 1 3]
Using np.nonzero() – Locate Non-Zero Values
arr = np.array([0, 3, 0, 4, 5])
indices = np.nonzero(arr)
print(indices)
Output:
(array([1, 3, 4]),)
Use for sparse data or finding meaningful entries
Using np.isin() – Membership Testing
arr = np.array([10, 20, 30, 40])
test = np.isin(arr, [20, 40])
print(test)
Output:
[False True False True]
Great for filtering arrays based on membership
Using np.argmax() and np.argmin()
arr = np.array([1, 7, 3, 9, 2])
print(np.argmax(arr)) # Output: 3 (value 9)
print(np.argmin(arr)) # Output: 0 (value 1)
Quickly get index of highest or lowest value
Multi-Dimensional where() Usage
matrix = np.array([[1, 2, 3],
[4, 5, 6]])
result = np.where(matrix > 3)
print(result)
Output:
(array([1, 1, 1]), array([0, 1, 2]))
First array → row indices, second array → column indices
Search Function Comparison Table
| Function | Description | Output Type |
|---|---|---|
np.where() | Indices or condition-based replacement | tuple of indices |
np.searchsorted() | Index to insert into sorted array | int or array of int |
np.nonzero() | Indices of non-zero values | tuple of indices |
np.isin() | Boolean mask of membership test | boolean array |
np.argmax() | Index of max value | int |
np.argmin() | Index of min value | int |
Summary – Key Takeaways
- Use
where()for filtering and condition-based updates - Use
searchsorted()for insertion into sorted arrays - Use
nonzero()to locate all non-zero elements - Use
isin()to test membership quickly - Use
argmax()andargmin()for finding extremes
Real-World Applications
- Filter sensor values that exceed thresholds
- Search time series events with
where() - Locate anomalies in financial or medical data
- Build fast search systems using
searchsorted() - Compare datasets using
isin()for joins
FAQs – NumPy Array Search
How do I find all indices where a condition is met?
Use np.where():
np.where(arr > 50)
What’s the difference between where() and nonzero()?
nonzero() only returns indices of non-zero values. where() lets you specify any condition.
Can I replace values with where()?
Yes:
np.where(arr > 0, 1, 0)
Does searchsorted() work on unsorted arrays?
No. It only gives correct results if the array is sorted.
How do I check if values are in another array?
Use np.isin():
np.isin(arr1, arr2)
Share Now :