Michael Yang
Michael Yang

Reputation: 13

How do I delete rows in a numpy array based on a single column?

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

Answers (3)

Djaouad
Djaouad

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

Alvaro Cuervo
Alvaro Cuervo

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

Georgina Skibinski
Georgina Skibinski

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

Related Questions