Reputation: 13
I have a 2d numpy array of floats, and I want to delete all rows in which the third column of that row contains a value less than x
.
eg. [[3,4,5],[3,3,8],[4,2,1],[1,2,1]]
, with threshold 2
, outputs [[3,4,5],[3,3,8]]
.
Upvotes: 0
Views: 388
Reputation: 22766
You can use a list-comprehension:
import numpy as np
arr = np.array([[3,4,5],[3,3,8],[4,2,1],[1,2,1]])
threshold = 2
arr = np.array([row for row in arr if row[2] >= threshold])
print(arr)
Output:
[[3 4 5]
[3 3 8]]
Alternatively, you can use filter
:
np.array([*filter(lambda r : r[2] >= threshold, arr)])
Upvotes: 0
Reputation: 104
Try this:
import numpy as np
array = np.array([[3,4,5],[3,3,8],[4,2,1],[1,2,1]])
array = np.array([x for x in array if x[2] > 2])
print (array)
Upvotes: 0
Reputation: 13387
Try this one:
>>> import numpy as np
>>> x=np.array([[3,4,5],[3,3,8],[4,2,1],[1,2,1]])
>>> x=x[x[:,2]>=2]
>>> x
array([[3, 4, 5],
[3, 3, 8]])
Upvotes: 2