How to filter an array using NUmpy?
Ask by ICSM Computer
Updated 23 Nov 2025
To filter (select) values from a NumPy array, you use boolean indexing — create a boolean condition and apply it to the array.
Basic Example
Output
Filter Using Multiple Conditions
Use
&(AND),|(OR), and~(NOT).Parentheses are required.
Output:
Filter with Another Array of Booleans
Output:
Filter Strings
To filter strings containing a substring:
Filter 2D Arrays
Output:
Replace filtered values instead of selecting
Output:
Get indexes of matches
Result:
Summary
arr[arr > 3]arr[(arr >= 10) & (arr <= 20)]arr[mask]arr[arr > 10] = 0np.where(arr == x)