pbreach
pbreach

Reputation: 16987

How do I search for indices that satisfy condition in numpy?

I have columns corresponding to a given day, month, and year in a numpy array called 'a' and I am comparing all three of these values to the columns of another array called 'b' which also correspond to day,month, and year to find the index of 'a' that is equal to 'b' so far I have tried:

a[:,3:6,1] == b[1,3:6]

array([[False,  True,  True],
   [ True,  True,  True],
   [False,  True,  True],
   ..., 
   [False, False, False],
   [False, False, False],
   [False, False, False]], dtype=bool)

which works fine but I need the row that corresponds to [True,True,True] I've also tried:

np.where(a[:,3:6,1] == b[1,3:6], a[:,3:6,1])

ValueError: either both or neither of x and y should be given

and

a[:,:,1].all(a[:,3:6,1] == b[1,3:6])

TypeError: only length-1 arrays can be converted to Python scalars

What is a quick and easy way to do this?

Upvotes: 2

Views: 1197

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58865

You can use np.all() along the last axis:

rows = np.where((a[:,3:6,1]==b[1,3:6]).all(axis=1))[0]

it will store in rows the indices where all the row contains True values.

Upvotes: 3

Related Questions