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)])
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.