Reputation: 239
I have a 2D numpy array in the following form:
array([[0, 4],
[1, 5],
[2, 6]])
I want to filter out the rows that their first value is bigger than 1, but I couldn't find a numpy function to do so.
I know that I can use filter
:
np.array(list(filter((lambda x: x[0] <= 1), my_arr)))
This approach is not efficient, since I need to convert the result into list and only than into numpy array. Is there a better way?
Upvotes: 2
Views: 834
Reputation: 1
import numpy as np
arr=np.array([[0, 4],
[1, 5],
[2, 6]])
newarr=[]
for fetch in arr:
if(fetch[0]>1):
newarr.append(fetch)
print(newarr)
Upvotes: 0
Reputation: 95873
There is not numpy
interface to do this efficiently with a function. However, in this particular case, you just want something like:
>>> import numpy as np
>>> arr = np.array([[0, 4],
... [1, 5],
... [2, 6]])
>>> arr[arr[:,0] <= 1]
array([[0, 4],
[1, 5]])
Upvotes: 2