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 - When the np.where() function is applied to a 2-D numpy array, a tuple containing two arrays is returned. These arrays correspond to respective indices of an element in both dimensions.
Discussions

using np.where on a 2D array
Do an all-reduction after comparing the two arrays: index_that_matches = np.where((reduced == vector).all(1))[0][0] More on reddit.com
๐ŸŒ r/learnpython
3
2
September 5, 2022
python - Using np.where to find matching row in 2D array - Stack Overflow
The problem with your approach is that numpy broadcasts the arrays (visualized with np.broadcast_arrays): More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 23, 2017
python - Numpy where for 2 dimensional array - Stack Overflow
I have a 2 d numpy array. I need to keep all the rows whose value at a specific column is greater than a certain number. Right now, I have: f_left = np.where(f_sorted[:,attribute] >= split_p... More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 19, 2016
How to find the index of a value in 2d array in Python? - Stack Overflow
Is it possible to do something similar but with the index where the 2d array is nearest to a given value, e.g. np.where_is_nearest_to(0.99)? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.where.html
numpy.where โ€” NumPy v2.5 Manual
>>> x, y = np.ogrid[:3, :4] >>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast array([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]])
๐ŸŒ
Kanoki
kanoki.org โ€บ 2020 โ€บ 01 โ€บ 03 โ€บ how-to-work-with-numpy-where
How to work with numpy.where() | kanoki
January 3, 2020 - First create an Array ยท x = np.arange(9) Output ยท array([ 0, 1, 4, 9, 16, 25, 6, 7, 8]) Now we will Square all the elements in array which is greater than 5 ( x>5 ) np.where(x>5, x**2, x) Output ยท array([ 0, 1, 2, 3, 4, 5, 36, 49, 64]) The output is a ndarray where all the elements >5 are squared and elements <5 remains same ยท
๐ŸŒ
OpenSourceOptions
opensourceoptions.com โ€บ how-to-use-numpy-where-in-python-with-examples
How to Use numpy.where() in Python with Examples โ€“ OpenSourceOptions
Below we use the result of the ... add the value-if-true and value-if-false parameters to the np.where call. This returns a 2D array with the same shape as the conditional array (a2d)....
๐ŸŒ
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 โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.where.html
numpy.where โ€” NumPy v2.2 Manual
>>> x, y = np.ogrid[:3, :4] >>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast array([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]])
Find elsewhere
๐ŸŒ
Python Central
pythoncentral.io โ€บ numpy-np-where-using-conditional-array-operations
NumPy where(): Using Conditional Array Operations | Python Central
January 19, 2025 - 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 within the array with their corresponding absolute values.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-numpy-where
How to use Python numpy.where() Method | DigitalOcean
August 3, 2022 - Leverage NumPyโ€™s where() function to efficiently select elements from arrays based on conditions, creating new arrays with tailored values.
๐ŸŒ
Tudelft
oit.tudelft.nl โ€บ learn-python โ€บ 2025 โ€บ numpy โ€บ 2d.html
1.2. Two-Dimensional arrays and matrices โ€” Python for Civil Engineers
So, np.where() can be used in two ways. Either to return a tuple with the index of values that fall under the specified condition; or, to return a modified array following the specified condition.
๐ŸŒ
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
So far so good: the first array to be returned is the row idx's of where integers 4,5,6 appear in a, and the second array is the col idx's of where 4,5,6 appear in a. Copy>>> np.where(a == [4,5,8]) (array([0, 0]), array([0, 1]))
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.where.html
numpy.where โ€” NumPy v2.6.dev0 Manual
>>> x, y = np.ogrid[:3, :4] >>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast array([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]])
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_search.asp
NumPy Searching Arrays
To search an array, use the where() method. ... import numpy as np arr = np.array([1, 2, 3, 4, 5, 4, 4]) x = np.where(arr == 4) print(x) Try it Yourself ยป
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ 2d-array-in-numpy
2D Arrays in NumPy (Python)
October 28, 2022 - In this article, we have explored 2D array in Numpy in Python. Numpy is a Python library for numerical computations and has a good support for multi-dimensional arrays.
๐ŸŒ
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.
๐ŸŒ
Medium
medium.com โ€บ @bouimouass.o โ€บ what-does-a-numpy-2d-array-look-like-in-python-38752ccde895
What does a numpy 2D array look like in python? | by Omar | Medium
July 23, 2023 - For example, the following code creates a 2D NumPy array with 3 rows and 4 columns using the arange() and reshape() functions: import numpy as np # Create a sequence of numbers numbers = np.arange(12) # Reshape the sequence into a 2D array array = numbers.reshape(3, 4) # Print the array print(array) This code will print the same array as the previous code.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ numpy: modify elements in 2d-array that correspond to specific positions in another two arrays
r/learnpython on Reddit: NumPy: Modify elements in 2D-array that correspond to specific positions in another two arrays
April 3, 2023 -

Hi!
I am pretty new to NumPy and have a challenge that is hard to explain in words, I will try as clearly as possible.
I have three 2D-arrays with the same shape (300, 300): "change_array", "weight_array" and "length_array". Total elements in each array is 90k.
From the weight_array and length_array I want to extract the element positions where weight AND length > 10. Maybe only 25 out of 90k elements have length AND weight over 10.
When I have these element positions, I want to refer to them in the change_array and change its corresponding elements to a numerical value (I run a calculation for them). All other values should be NaN values.

How would you go about to do this? I have a code suggestion, but it is errendous and may become misleading of what my goal is.