For the general case, where your search string can be in any column, you can do this:

>>> rows, cols = np.where(t == 'bar')
>>> t[rows]
array([['2', '3', '4', 'bar'],
       ['8', '9', '1', 'bar']],
      dtype='|S11')
Answer from Jaime on Stack Overflow
🌐
Machine Learning Plus
machinelearningplus.com › blog › how to use numpy where function?
How to Use Numpy Where Function? - machinelearningplus
January 26, 2023 - Step 1: Create a 2-D numpy array · python · a_2d_array = np.array([[0, -1], [1, 3], ]) Step 2: Use np.where() function to get the position of element -1 · python · np.where(a_2d_array == -1) python ·
🌐
Reddit
reddit.com › r/learnpython › using np.where on a 2d array
r/learnpython on Reddit: using np.where on a 2D array
September 5, 2022 -

I have a 2D array that is shaped like (10000,2). The rows in this array repeat so I call np.unique with axis=0, to get the 2D array of all the unique rows. This reduces the size down to (24,2) or something like that. Now, I have a vector and I want to find out which index in the reduced array it is equal to. So I call np.where(reduced == vector). This does not give me the row that it is equal. Rather it takes the input vector 1 number at a time and finds where in the reduced array the numbers match. This is OK since when there are two numbers that are the same consecutively, that equates to the index that I want. So I just call np.ediff1d and find where that equals 0. I was wondering if there is an easier way to do this? Thanks. Example code is given.

import numpy as np

reduced = [[10,2],[15,3],[10,1],[16,5],[16,3]]
vector = [15,3]

results = np.where(reduced == vector)

# I want the output to be 1 but I get 
# results[0] = [1,1,4]
diffs = np.ediff1d(results[0]) 

# this results in [0,3]
index_that_matchs = np.where(diffs == 0)[0][0]
index = results[0][index_that_matchs]

# this results in 1
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
>>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
Kanoki
kanoki.org › 2020 › 01 › 03 › how-to-work-with-numpy-where
How to work with numpy.where() | kanoki
January 3, 2020 - numpy.where(condition[, x, y]) · Return elements chosen from x or y depending on condition
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.where.html
numpy.where — NumPy v2.2 Manual
>>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
OpenSourceOptions
opensourceoptions.com › how-to-use-numpy-where-in-python-with-examples
How to Use numpy.where() in Python with Examples – OpenSourceOptions
This returns a 2D array with the same shape as the conditional array (a2d). As we did with the 1D array example, the value-if-true and value-if-false parameters can also be arrays instead of single values. ... The numpy.where function is very powerful and should be used to apply if/else and ...
🌐
Python Central
pythoncentral.io › numpy-np-where-using-conditional-array-operations
NumPy where(): Using Conditional Array Operations | Python Central
January 19, 2025 - This Python code snippet utilizes NumPy to process a 2D array. It first creates a NumPy array named "matrix" containing both positive and negative integers. Then, it employs the np.where() function to efficiently replace all negative values ...
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.where.html
numpy.where — NumPy v2.0 Manual
Return elements chosen from x or y depending on condition · When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero(). Using nonzero directly should be preferred, as it behaves correctly for subclasses. The rest of this documentation covers only the ...
🌐
Medium
medium.com › @heyamit10 › understanding-numpy-where-with-two-conditions-21871ed01aa6
Understanding numpy.where() with Two Conditions | by Hey Amit | Medium
March 6, 2025 - You might be wondering, “Can I apply these conditions to 2D arrays too?” Absolutely! Let’s see how it works. Scenario: In a matrix, keep values that are greater than 5 and less than 15. Replace others with 0. Code Example: import numpy as np # Step 1: Create a 2D NumPy array (matrix) matrix = np.array([[4, 10, 15], [20, 5, 7]]) # Step 2: Apply conditions across the matrix result = np.where((matrix > 5) & (matrix < 15), matrix, 0) # Step 3: Display the modified matrix print(result) Output: [[ 0 10 0] [ 0 0 7]] Breakdown: Conditions: matrix > 5 and matrix < 15 ·
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.where.html
numpy.where — NumPy v2.6.dev0 Manual
>>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
Stack Overflow
stackoverflow.com › questions › 34667493 › numpy-where-with-a-2d-array-and-a-1d-array-in-the-condition › 34667739
python - numpy.where() with a 2d array and a 1d array in the condition - Stack Overflow
Given the following 2d numpy array: Copy>>> a = np.arange(4,10).reshape(2,3) array([[4, 5, 6], [7, 8, 9]]) we can use np.where() to get indices of entries satisfying certain conditions.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.where.html
numpy.where — NumPy v2.1 Manual
>>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
Python Guides
pythonguides.com › python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 9, 2025 - Create and use a 2D array in Python with nested lists and NumPy: zeros, reshape, indexing rows and columns, adding rows, user input and the [[0]*n]*m trap.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
numpy.where(): Manipulate elements depending on conditions | note.nkmk.me
November 5, 2019 - source: numpy_where.py · In this case, it means that the elements at [0, 0], [0, 1], [0, 2] and [1, 0] satisfy the condition. It is also possible to obtain a list of each coordinate by using list(), zip(), and * as follows: Transpose 2D list in Python (swap rows and columns) print(list(zip(*np.where(a < 4)))) # [(0, 0), (0, 1), (0, 2), (1, 0)] source: numpy_where.py ·
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_where_function.htm
Numpy where() Function
import numpy as np my_array = np.array([[1, 2], [3, 4], [5, 6]]) result = np.where(my_array > 3, 0, my_array) print("2D Array after replacement:\n", result) Following is the output of the above code − · 2D Array after replacement: [[1 2] [3 0] [0 0]] numpy_array_manipulation.htm ·
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.where.html
numpy.where — NumPy v1.13 Manual
NumPy v1.13 Manual · NumPy Reference · Routines · Indexing routines · index · next · previous · numpy.where(condition[, x, y])¶ · Return elements, either from x or y, depending on condition. If only condition is given, return condition.nonzero(). See also · nonzero, choose ·
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
NumPy is used to work with arrays. The array object in NumPy is called ndarray.