Home > Software design >  How to remove rows from numpy array if certain number of an element is present
How to remove rows from numpy array if certain number of an element is present

Time:08-24

I have a 2d numpy array that contains some numbers like:

data = 
[[1.1, 1.2, 1.3, 1.4],
[2.1, 2.2, 2.3, -1.0],
[-1.0, 3.2, 3.3, -1.0],
[-1.0, -1.0. -1.0, -1.0]]

I want to remove every row that contains the value -1.0 2 or more times, so I'm left with

data = 
[[1.1, 1.2, 1.3, 1.4],
[2.1, 2.2, 2.3, -1.0]]

I found this question which looks like it's very close to what I'm trying to do, but I can't quite figure out how I can rewrite that to fit my use case.

CodePudding user response:

You can easily do it with this piece of code:

new_data = data[(data == -1).sum(axis=1) < 2]

Result:

>>> new_data
array([[ 1.1,  1.2,  1.3,  1.4],
       [ 2.1,  2.2,  2.3, -1. ]])

CodePudding user response:

def remove_rows(data, threshold):
    mask = np.array([np.sum(row == -1) < threshold for row in data])
    return data[mask]

This function will return a new array with no rows having -1's more than or equal to the threshold

You need to pass in a Numpy array for it to work.

  • Related