Numpy:按多个条件过滤行?

ste*_*ten 7 python numpy conditional-statements

我有一个meta用3列调用的二维numpy数组..我想要做的是:

  1. 检查前两列是否为ZERO
  2. 检查第三列是否小于X.
  3. 仅返回与条件匹配的行

我做到了,但解决方案看起来非常人为:

meta[ np.logical_and( np.all( meta[:,0:2] == [0,0],axis=1 ) , meta[:,2] < 20) ]
Run Code Online (Sandbox Code Playgroud)

你能想到更清洁的方式吗?似乎很难同时拥有多个条件;(

谢谢


对不起我第一次复制了错误的表达...纠正了.

rep*_*cus 12

您可以在切片中使用多个过滤器,如下所示:

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