Sadia Khalil
Sadia Khalil

Reputation: 1

removing an entire row with conditions on a column

Following the question and solution posted here:

How could I remove the rows of an array if one of the elements of the row does not satisfy a condition?

I like to ask how to delete a row with a combined operator condition on a column value. In short, I like to delete all the rows whose 3rd column values are not between 7 and 15.

print (data[:,2])
to_remove = data[:,2] < 7 and  data[:,2] >= 15

The above line is not allowed and it throws a value error.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Upvotes: 0

Views: 81

Answers (3)

fiveobjects
fiveobjects

Reputation: 4347

to_remove = (data[:,2] < 7) | (data[:,2] >= 15)

Upvotes: 0

basilisk
basilisk

Reputation: 1277

I think you need something like this :

   result = []
   for row in arr:  # loop over the rows
      if row[2] > 7 and row[2] < 15: # this is the condition you need 
         result.append(row) # store the rows which third column is between 7 and 15 in a new array

   print(result)

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71620

Try:

to_remove = (data[:,2] < 7) | (data[:,2] > 14)

Upvotes: 1

Related Questions