-2

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:

  1. The 3rd col's value needs to be 1. The 4th needs needs to be 0.
  2. 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])

2
  • first time I'm using stack overflow ... but my feeling is this should be a one liner and not a for loop. i've been trying to get my head around the syntax in here for example: stackoverflow.com/questions/29501823/… but can't seem to work out how to apply it. Commented Feb 9, 2018 at 14:44
  • Thx Piinthesky ... I'm still getting my bearings with stack overflow. :) Just posted my A. I guess I need a couple of days to say it's my preference. Commented Feb 9, 2018 at 16:07

3 Answers 3

0

Normally you should give the code you tried and then we try to correct it, but I'm in a good mood, so here you go:

import numpy as np

arr = np.asarray(([1, 2, 1, 0, 3],[1,1,1,1,1],[2,2,2,2,2],[1,0,1,0,1]))

idx = [i for i in range(len(arr)) if arr[i,2]==1 and arr[i,3]==0]

print(idx)
Sign up to request clarification or add additional context in comments.

2 Comments

first time I'm using stack overflow ... but my feeling is this should be a one liner and not a for loop. i've been trying to get my head around the syntax in here for example: stackoverflow.com/questions/29501823/… but can't seem to work out how to apply it.
Thanks ViG ... I'm learning how to be a better stack overflow participant in this, I apologize for revising my Q two times now. But my issue in this is that the list of columns isn't set. Nor the amount of them. Nor are the values set. They'll be coming down as list vars to whatever I use to do the filtering. I could accommodate your solution, but it would require creating a more dynamic for loop. I'm still feeling like there might be a one liner here I'm missing.
0

In python you can use enumerate() to return a list containing idx and element

import numpy as np

arr = np.asarray(([1, 2, 1, 0, 3],[1,1,1,1,1],[2,2,2,2,2],[1,0,1,0,1]))

list = []
for idx, element in enumerate(arr):
    if(element[2] == 1 and element[3] == 0):
        list.append(idx)

print(list)

1 Comment

thanks for the idea. I'm looking for a one-liner. I've revised my Q just slightly to give more context.
0

(answer to my own Q) I think this is what I'm looking for...

colIdx = [2,3]
vals = [1,0]
np.argwhere(np.all(arr[:,colIdx] == vals,axis=1)==True)[:,0]

Response:

array([0, 3])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.