You can use numpy.extract:
>>> nparr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> nparreven = np.extract(nparr % 2 == 0, nparr)
or numpy.where:
>>> nparr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> nparreven = nparr[np.where(nparr % 2 == 0)]
Answer from unlut on Stack OverflowW3Schools
w3schools.com › python › numpy › numpy_array_filter.asp
NumPy Filter Array
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) # Create an empty list filter_arr = [] # go through each element in arr for element in arr: # if the element is completely divisble by 2, set the value to True, otherwise False if element % 2 == 0: filter_arr.append(True) else: filter_arr.append(False) newarr = arr[filter_arr] print(filter_arr) print(newarr) Try it Yourself » · The above example is quite a common task in NumPy and NumPy provides a nice way to tackle it. We can directly substitute the array instead of the iterable variable in our condition and it will work just as we expect it to.
DataCamp
datacamp.com › doc › numpy › filtering-arrays
NumPy Filtering Arrays
In this syntax, condition is a boolean array that determines which elements from array are included in filtered_array. import numpy as np array = np.array([1, 2, 3, 4, 5]) condition = array > 3 filtered_array = array[condition] print(filtered_array) # Output: [4, 5]
08:11
Filtering in NumPy is easy! 🧲 - YouTube
03:14
numpy filter by condition - YouTube
06:52
NumPy Filter Array | Filter a NumPy Array | Filter rows of a NumPy ...
05:03
Filter Row of A Numpy Array | Numpy Tutorial - YouTube
07:52
Python NumPy Tutorial For Beginners - NumPy Direct Filtering - YouTube
Top answer 1 of 2
15
You can use numpy.extract:
>>> nparr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> nparreven = np.extract(nparr % 2 == 0, nparr)
or numpy.where:
>>> nparr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> nparreven = nparr[np.where(nparr % 2 == 0)]
2 of 2
8
Since you don't provide an example data, use toy data:
# Cost of agents represented by indices of cost, we have agents 0, 1, 2, 3
cost = np.array([4,5,6,2])
# Agents to consider
np_agents = np.array([0,1,3])
# threshold for each agent. Calculate different thresholds for different agents. Use array of indexes np_agents into cost array.
thresholds = cost[np_agents] # np.array([4,5,2])
feasible_agents = np_agents[np_agents > thresholds] # np.array([3])
GeeksforGeeks
geeksforgeeks.org › python › numpy-filtering-rows-by-multiple-conditions
NumPy - Filtering rows by multiple conditions - GeeksforGeeks
October 10, 2022 - # importing numpy lib import numpy as np # making a numpy array arr = np.array([x for x in range(11, 40)]) print("Original array") print(arr) # making a blank list new_arr = [] for x in arr: # applying two conditions: number is divisible by 2 and is greater than 15 if x % 2 == 0 and x > 15: new_arr.append(x) # Converting new list into numpy array new_arr = np.array(new_arr) print("New array") print(new_arr) ... # importing numpy lib import numpy as np # making a numpy array arr = np.array([x for x in range(11, 40)]) print("Original array") print(arr) # using lambda to apply condition new_arr = list(filter(lambda x: x > 15 and x % 2 == 0 and x % 10 != 0, arr)) # Converting new list into numpy array new_arr = np.array(new_arr) print("New array") print(new_arr)
GeeksforGeeks
geeksforgeeks.org › python › how-to-filter-two-dimensional-numpy-array-based-on-condition
How to filter two-dimensional NumPy array based on condition ? - GeeksforGeeks
July 23, 2025 - To filter we used this fltr in numpy.in1d() method and stored as its values in the original array that return True if condition fulfills.
Python Guides
pythonguides.com › python-numpy-filter
How To Filter NumPy 2D Array By Condition In Python
May 16, 2025 - Boolean indexing is the easiest way to filter a 2D array in Python NumPy. It works by creating a mask of True/False values and using it to select elements. Let’s create a simple 2D array representing sales data for different store locations:
Sling Academy
slingacademy.com › article › numpy-how-to-filter-an-array-by-a-condition
NumPy: How to Filter an Array by a Condition - Sling Academy
At its simplest, filtering can be done with comparison operators. When you perform a comparison operation on an array, you get a boolean array that you can use to select elements. import numpy as np # Create a NumPy array arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Condition for filtering ...
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.where.html
numpy.where — NumPy v2.2 Manual
>>> np.where([[True, False], [True, True]], ... [[1, 2], [3, 4]], ... [[9, 8], [7, 6]]) array([[1, 8], [3, 4]]) The shapes of x, y, and the condition are broadcast together:
TutorialsPoint
tutorialspoint.com › numpy › numpy_filtering_arrays.htm
NumPy - Filtering Arrays
In the following example, we are filtering elements greater than the value "10" from the given array − · import numpy as np # Creating an array array = np.array([1, 5, 8, 12, 20, 3]) # Define the condition condition = array > 10 # Apply the condition to filter the array filtered_array = array[condition] print("Original Array:", array) print("Filtered Array (elements > 10):", filtered_array)
Codecademy
codecademy.com › learn › introduction-to-numpy-for-finance › modules › learn-numpy-introduction-for-finance › cheatsheet
Introduction to NumPy: Learn NumPy: Introduction Cheatsheet | Codecademy
Level up in financial analytics by learning Python to process, analyze, and visualize financial data. ... NumPy elements can be indexed using conditionals. The syntax to filter an array using a conditional is array_name[conditional].
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
>>> np.where([[True, False], [True, True]], ... [[1, 2], [3, 4]], ... [[9, 8], [7, 6]]) array([[1, 8], [3, 4]]) The shapes of x, y, and the condition are broadcast together:
Arab Psychology
scales.arabpsychology.com › home › how to easily filter numpy arrays: a step-by-step guide
How To Easily Filter NumPy Arrays: A Step-by-Step Guide
November 29, 2025 - You can use the following methods to filter the values in a NumPy array: ... This is the most straightforward technique, utilizing direct Boolean comparison to select elements. The expression returns a Boolean array, which is then used as an index to filter the original array, keeping only those elements where the condition evaluated to True.