I have a two-dimensional numpy array called meta
with 3 columns.. what I want to do is :
我有一个名为meta with 3 columns的二维numpy数组..我想要做的是:
- check if the first two columns are ZERO
- check if the third column is smaller than X
- Return only those rows that match the condition
检查前两列是否为ZERO
检查第三列是否小于X.
仅返回与条件匹配的行
I made it work, but the solution seem very contrived :
我使它工作,但解决方案似乎非常做作:
meta[ np.logical_and( np.all( meta[:,0:2] == [0,0],axis=1 ) , meta[:,2] < 20) ]
Could you think of cleaner way ? It seem hard to have multiple conditions at once ;(
你能想到更清洁的方式吗?似乎很难同时拥有多个条件;(
thanks
Sorry first time I copied the wrong expression... corrected.
对不起我第一次复制了错误的表达...纠正了。
2 个解决方案
#1
10
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 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.]])
#2
2
How about this -
这个怎么样 -
meta[meta[:,2]<X * np.all(meta[:,0:2]==0,1),:]
Sample run -
样品运行 -
In [89]: meta
Out[89]:
array([[ 1, 2, 3, 4],
[ 0, 0, 2, 0],
[ 9, 0, 11, 12]])
In [90]: X
Out[90]: 4
In [91]: meta[meta[:,2]<X * np.all(meta[:,0:2]==0,1),:]
Out[91]: array([[0, 0, 2, 0]])
#1
10
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 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.]])
#2
2
How about this -
这个怎么样 -
meta[meta[:,2]<X * np.all(meta[:,0:2]==0,1),:]
Sample run -
样品运行 -
In [89]: meta
Out[89]:
array([[ 1, 2, 3, 4],
[ 0, 0, 2, 0],
[ 9, 0, 11, 12]])
In [90]: X
Out[90]: 4
In [91]: meta[meta[:,2]<X * np.all(meta[:,0:2]==0,1),:]
Out[91]: array([[0, 0, 2, 0]])