Seems like an easy problem, and a couple Q's out there close to this ... but I just can't seem to find the answer.
Say I have a numpy 2d array:
> arr = np.asarray(([1, 2, 1, 0, 3],[1,1,1,1,1],[2,2,2,2,2],[1,0,1,0,1]))
> arr
array([[1, 2, 1, 0, 3],
[1, 1, 1, 1, 0],
[1, 0, 2, 2, 2],
[1, 0, 1, 0, 1]])
And I want to use two criteria to get me that first and fourth row. The criteria being:
- The 3rd col's value needs to be 1. The 4th needs needs to be 0.
- When the filter is met -- it gets me the idx of rows that meet that criteria in response.
So I should be getting something like this when it's done right...
Response: [0,3]
...
[more info about my question]
I instinctively feel the syntax should be a one liner, something like this:
colIdx = [2,3]
vals = [1,0]
np.argwhere(arr[:,colIdx]==vals)[:,0] #<--- ie: but doesn't work
Being able to accommodate two variables like colIdx and vals would really work for me -- because I'll have dynamically created lists for both the columns being checked (ie: 2,3) and the values (ie: 1,0) I'm looking for.
I could have multiple columns being checked beyond just two as well. And the values aren't set either. Thus the need for a dynamic approach.
The closest Q+A I've seen so far on stack overflow for this sort of Q, is here: Numpy: Filtering rows by multiple conditions? -- but can't seem to work out the syntax for my problem.
ANSWERING MY OWN Q:
colIdx = [2,3]
vals = [1,0]
np.argwhere(np.all(arr[:,colIdx] == vals,axis=1)==True)[:,0]
Response:
array([0, 3])