Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy: how to select rows based on a bunch of criteria

How can I fetch the rows for which the second column equals to 4 or 6?

a = np.array(np.mat('1 2; 3 4; 5 6; 7 4'))
b = [4,6]

Apparently, this does not work:

c = a[a[:,1] in b]
like image 495
user2295350 Avatar asked Oct 01 '13 08:10

user2295350


2 Answers

The numpythonic way of doing this would be to use in1d, something like:

a[np.in1d(a[:, 1], b)]
like image 154
Jaime Avatar answered Sep 28 '22 02:09

Jaime


You can do:

check = np.logical_or(a[:,1]==4, a[:,1]==6)
c = a[check,:]

You can also use | for the logical operator or:

check = (a[:,1]==4) | (a[:,1]==6)
like image 21
Saullo G. P. Castro Avatar answered Sep 28 '22 02:09

Saullo G. P. Castro