Reputation: 880
An easy Example for what i want:
I have a Matrix with many columns where i want to keep rows, if the cells in the first 2 columns are (both!!) bigger than 0.1
something like that:
import numpy as np
A=np.array([[1,0,1],[1,2,5],[0,3,5],[0,0,2]])
B=np.zeros(4,3)
wildcard = np.vstack((B, A[A[:,0 and 1] > 0.1]))
obviously the "and" does not work, but that is what i want- both values in each line should be checked before they are returned to a new array where just the rows with these conditions remain.
Upvotes: 0
Views: 76
Reputation: 10329
instead of using and you can use &. Extra parenthesis are required because & binds tighter than >
a[(a[:,0] > .1) & (a[:,1] > .1)]
The method proposed by Ophion in the comments works as well but is slower.
In [85]: b = np.random.random((100000,3))
In [86]: %timeit np.all(b[:,:2] > 0.1, axis = 1)
100 loops, best of 3: 2.99 ms per loop
In [87]: %timeit (b[:,0] > .1) & (b[:,1] > .1)
1000 loops, best of 3: 542 us per loop
Upvotes: 2