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]].
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]])
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)])