Getting the row index for a numPy 2D array when multiple column values ​​are known

Suppose I have a 2D numPy array, for example:

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

How to find the index of a row for which I know several values? For example, if it is known that the 0th column is 2 and the 1st column is 5, I would like to know the index of the row where this condition is satisfied (row 1 in this case).

In my application, the first two columns are (x, y) coordinates, and the third column is information about this coordinate. I am trying to find specific coordinates in a list, so I can change the value in the third column.

EDIT: For clarification, here is a non-square example:

a = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18]]

Suppose I know that the row I'm looking for has 13 in the 0th column and 14 in the 1st column. I would like to return the index of this row. In this case, I would like to return index 2 (2nd row).

Or even better, I would like to edit the 4th column of the row, which has 13 in the 0th column and 14 in the 1st column. Here is the solution I found in the case I described (changing the value to 999):

a [(a [ :, 0 ] == 13) and (a [ :, 1 ] == 14), 3] = 999

gives:

a = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 999, 17, 18]]

Sorry if this is unclear. Can someone point out in my original post (above edit) how this can be interpreted differently because I am having problems with this.

Thanks.

EDIT 2: Bug fixed on first editing (in bold)

Now I see how I made it all confusing for everyone. The solution to my problem is well described in condition b) of the food solution. Thanks.

+8
python numpy
source share
3 answers

Here are ways to handle conditions on columns or rows inspired by Zen Python.

In []: import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. ... 

So, following the second tip:
a) the conditions of the column (s) applied to the row (s):

 In []: a= arange(12).reshape(3, 4) In []: a Out[]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) In []: a[2, logical_and(1== a[0, :], 5== a[1, :])]+= 12 In []: a Out[]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 21, 10, 11]]) 

b) the conditions for the row (s) applied to the column (s):

 In []: a= aT In []: a Out[]: array([[ 0, 4, 8], [ 1, 5, 21], [ 2, 6, 10], [ 3, 7, 11]]) In []: a[logical_and(1== a[:, 0], 5== a[:, 1]), 2]+= 12 In []: a Out[]: array([[ 0, 4, 8], [ 1, 5, 33], [ 2, 6, 10], [ 3, 7, 11]]) 

Thus, I hope that it really makes sense to always be explicit when accessing columns and rows. The code is usually read by people with a different background.

+4
source share
 In [80]: a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) In [81]: a Out[81]: array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 

a==2 returns an array with a boolean number showing where the condition is True:

 In [82]: a==2 Out[82]: array([[False, True, False], [False, False, False], [False, False, False]], dtype=bool) 

You can find any columns where this value is True using np.any(...,axis=0) :

 In [83]: np.any(a==2,axis=0) Out[83]: array([False, True, False], dtype=bool) In [84]: np.any(a==5,axis=0) Out[84]: array([False, True, False], dtype=bool) 

You can find where both conditions are true at the same time using & :

 In [85]: np.any(a==2,axis=0) & np.any(a==5,axis=0) Out[85]: array([False, True, False], dtype=bool) 

Finally, you can find the column index where the conditions are True at the same time using np.where :

 In [86]: np.where(np.any(a==2,axis=0) & np.any(a==5,axis=0)) Out[86]: (array([1]),) 
+9
source share

Performance

 np.where(np.any(a==2,axis=0) & np.any(a==5,axis=0)) 

since the proposed unutbu will not use the information that is in the 0th column, and 5 in the 1st. So, for a = np.array([[5, 2, 3], [2, 5, 6], [7, 8, 9]]) it will incorrectly return (array([0, 1]),)

You can use instead

 np.where((a[0]==2) & (a[1]==5)) 

to get the correct result (array([1]),) .

Also, if you want to edit the second column of this particular row, you can skip np.where and just reference it: a[2][(a[0]==2) & (a[1]==5)] . This will work for assignments, for example a[2][(a[0]==2) & (a[1]==5)] = 11 .

+2
source share

All Articles