You can use a bool index array that you can produce using np.in1d.

You can index a np.ndarray along any axis you want using for example an array of bools indicating whether an element should be included. Since you want to index along axis=0, meaning you want to choose from the outest index, you need to have 1D np.array whose length is the number of rows. Each of its elements will indicate whether the row should be included.

A fast way to get this is to use np.in1d on the second column of a. You get all elements of that column by a[:, 1]. Now you have a 1D np.array whose elements should be checked against your filter. Thats what np.in1d is for.

So the complete code would look like:

import numpy as np

a = np.asarray([[2,'a'],[3,'b'],[4,'c'],[5,'d']])
filter = np.asarray(['a','c'])
a[np.in1d(a[:, 1], filter)]

or in a longer form:

import numpy as np

a = np.asarray([[2,'a'],[3,'b'],[4,'c'],[5,'d']])
filter = np.asarray(['a','c'])
mask = np.in1d(a[:, 1], filter)
a[mask]
Answer from jotasi on Stack Overflow
๐ŸŒ
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:
๐ŸŒ
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 - In this article, we are going to see how to apply the filter by the given condition in NumPy two-dimensional array. We have to obtain the output of required elements i.e., whatever we want to filter the elements from the existing array or new array. Here we are going to create a two-dimensional array in numpy. ... import numpy as np # 2-D Array also called as arrays # with rank 2 np_2d_arr = np.array([[1, 2, 3], [4, 5, 6]]) # View the 2-D Array A2 print(np_2d_arr)
Top answer
1 of 4
7

You can use a bool index array that you can produce using np.in1d.

You can index a np.ndarray along any axis you want using for example an array of bools indicating whether an element should be included. Since you want to index along axis=0, meaning you want to choose from the outest index, you need to have 1D np.array whose length is the number of rows. Each of its elements will indicate whether the row should be included.

A fast way to get this is to use np.in1d on the second column of a. You get all elements of that column by a[:, 1]. Now you have a 1D np.array whose elements should be checked against your filter. Thats what np.in1d is for.

So the complete code would look like:

import numpy as np

a = np.asarray([[2,'a'],[3,'b'],[4,'c'],[5,'d']])
filter = np.asarray(['a','c'])
a[np.in1d(a[:, 1], filter)]

or in a longer form:

import numpy as np

a = np.asarray([[2,'a'],[3,'b'],[4,'c'],[5,'d']])
filter = np.asarray(['a','c'])
mask = np.in1d(a[:, 1], filter)
a[mask]
2 of 4
3

A somewhat elaborate pure numpy vectorized solution:

>>> import numpy
>>> a = numpy.asarray([[2,'a'],[3,'b'],[4,'c'],[5,'d']])
>>> filter = numpy.array(['a','c'])
>>> a[(a[:,1,None] == filter[None,:]).any(axis=1)]
array([['2', 'a'],
       ['4', 'c']], 
      dtype='|S21')

None in the index creates a singleton dimension, therefore we can compare the column of a and the row of filter, and then reduce the resulting boolean array

>>> a[:,1,None] == filter[None,:]
array([[ True, False],
       [False, False],
       [False,  True],
       [False, False]], dtype=bool)

over the second dimension with any.

๐ŸŒ
W3Schools
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.
๐ŸŒ
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:
๐ŸŒ
ProjectPro
projectpro.io โ€บ recipes โ€บ filter-numpy-array-based-on-two-or-more-conditions
How to filter a numpy array based on two or more conditions? -
May 25, 2022 - Here we can see the array has been filtered, as we have pass a condition where if the values are than "65" append that values and exclude the values which are less than "65".
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to filter a numpy array (4 examples)
How to Filter a NumPy Array (4 Examples)
July 9, 2022 - Note: You can find the complete documentation for the NumPy in1d() function here. The following tutorials explain how to perform other common filtering operations in Python: How to Filter Pandas DataFrame Rows that Contain a Specific String ...
๐ŸŒ
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) # 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)
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ hey delphi
Array : Python numpy filter two-dimensional array by condition - YouTube
Array : Python numpy filter two-dimensional array by conditionTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"I promised to s...
Published: May 1, 2023
Views: 22
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_filtering_arrays.htm
NumPy - Filtering Arrays
The resulting Boolean array, representing the combined conditions, is then used to index the original array, extracting the elements that satisfy all specified criteria. In this example, we are filtering elements within a range using multiple conditions โˆ’ ยท import numpy as np # Creating an array array = np.array([1, 5, 8, 12, 20, 3]) # Define multiple conditions condition = (array > 5) & (array < 15) # Apply the conditions to filter the array filtered_array = array[condition] print("Original Array:", array) print("Filtered Array (5 < elements < 15):", filtered_array)
๐ŸŒ
Kanoki
kanoki.org โ€บ 2020 โ€บ 01 โ€บ 03 โ€บ how-to-work-with-numpy-where
How to work with numpy.where() | kanoki
January 3, 2020 - In this post we have seen how numpy.where() function can be used to filter the array or get the index or elements in the array where conditions are met ยท Additionally, We can also use numpy.where() to create columns conditionally in a pandas datafframe ยท Twitter Facebook LinkedIn ยท
๐ŸŒ
Data Science Parichay
datascienceparichay.com โ€บ home โ€บ blog โ€บ filter a numpy array โ€“ with examples
Filter a Numpy Array - With Examples - Data Science Parichay
June 17, 2022 - You can filter a numpy array by creating a list or an array of boolean values indicative of whether or not to keep the element in the corresponding array. This method is called boolean mask slicing.
๐ŸŒ
Lfppl
lfppl.com โ€บ numpy_array_filter
Python Data Science NumPy Filter Array
We cannot provide a description for this page right now
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ numpy-1.13.0 โ€บ reference โ€บ generated โ€บ numpy.extract.html
numpy.extract โ€” NumPy v1.13 Manual
This is equivalent to np.compress(ravel(condition), ravel(arr)). If condition is boolean np.extract is equivalent to arr[condition]. Note that place does the exact opposite of extract. ... >>> arr = np.arange(12).reshape((3, 4)) >>> arr array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) ...
๐ŸŒ
Cornell Virtual Workshop
cvw.cac.cornell.edu โ€บ PyDataSci1 โ€บ filtering_data
Cornell Virtual Workshop: Slicing & Filtering Data
Even after you have cleaned and preprocessed your data in various ways, you might want to further filter it, perhaps to extract a subset of the data for some specific processing. In earlier material on arrays and dataframes, we discussed the process of selecting a subset of data by indexing ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 40642253 โ€บ selecting-rows-from-2d-numpy-array-given-a-condition-from-a-function
python - Selecting rows from 2d numpy array given a condition from a function - Stack Overflow
I see a lot of examples using np.where() that work well when given the following: x = np.array([...]) w = np.where(x > 5) What if I need to filter the elements based on a function, like this? ...
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: Extract or delete elements, rows, and columns that satisfy the conditions | note.nkmk.me
May 31, 2019 - print(a < 2) # [[ True True False False] # [False False False False] # [False False False False]] print(np.where(a < 2)) # (array([0, 0]), array([0, 1])) print(np.where(a < 2)[0]) # [0 0] print(np.where(a < 2)[1]) # [0 1] ... See also the following article for np.where(). numpy.where(): Manipulate elements depending on conditions ยท By combining these two functions, you can delete the rows and columns that satisfy the condition.