0

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

3 Answers 3

2

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

upvoted..as a side note this works if there is a 2 in any column (if OP is interested): x[np.all(x>2,axis=1)]
0

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.