Numpy: Filtering rows by multiple conditions? Numpy: Filtering rows by multiple conditions? numpy numpy

Numpy: Filtering rows by multiple conditions?


you can use multiple filters in a slice, something like this:

x = np.arange(90.).reshape(30, 3)#set the first 10 rows of cols 1,2 to be zerox[0:10, 0:2] = 0.0x[(x[:,0] == 0.) & (x[:,1] == 0.) & (x[:,2] > 10)]#should give only a few rowsarray([[  0.,   0.,  11.],       [  0.,   0.,  14.],       [  0.,   0.,  17.],       [  0.,   0.,  20.],       [  0.,   0.,  23.],       [  0.,   0.,  26.],       [  0.,   0.,  29.]])


How about this -

meta[meta[:,2]<X * np.all(meta[:,0:2]==0,1),:]

Sample run -

In [89]: metaOut[89]: array([[ 1,  2,  3,  4],       [ 0,  0,  2,  0],       [ 9,  0, 11, 12]])In [90]: XOut[90]: 4In [91]: meta[meta[:,2]<X * np.all(meta[:,0:2]==0,1),:]Out[91]: array([[0, 0, 2, 0]])