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 OverflowFor 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')
You have to slice the array to the col you want to index:
rows = np.where(t[:,3] == 'bar')
result = t[rows]
This returns:
[[2,3,4,'bar'],
[8,9,1,'bar']]
using np.where on a 2D array
python - Using np.where to find matching row in 2D array - Stack Overflow
python - Numpy where for 2 dimensional array - Stack Overflow
How to find the index of a value in 2d array in Python? - Stack Overflow
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
You actually don't even need where.
yy = np.array(range(12)).reshape((4,3))
yy[yy[:,1] > 2]
Output
array([[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
x = np.array([[2,3,4],[5,6,7],[1,2,3],[8,9,10]])
array([[ 2, 3, 4],
[ 5, 6, 7],
[ 1, 2, 3],
[ 8, 9, 10]])
Find the rows where the second element are >=4
x[np.where(x[:,1] >= 4)]
array([[ 5, 6, 7],
[ 8, 9, 10]])
You can use np.where to return a tuple of arrays of x and y indices where a given condition holds in an array.
If a is the name of your array:
Use with AI>>> np.where(a == 1)
(array([0, 0, 1, 1]), array([0, 1, 2, 3]))
If you want a list of (x, y) pairs, you could zip the two arrays:
Use with AI>>> list(zip(*np.where(a == 1)))
[(0, 0), (0, 1), (1, 2), (1, 3)]
Or, even better, @jme points out that np.asarray(x).T can be a more efficient way to generate the pairs.
Using numpy, argwhere may be the best solution:
Use with AIimport numpy as np
array = np.array([[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 0, 0]])
solutions = np.argwhere(array == 1)
print(solutions)
>>>
[[0 0]
[0 1]
[1 2]
[1 3]]
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.