Skip to content
Advertisement

Filtering out rows in multidimensional numpy arrays

Let’s say that I have an array like this:

JavaScript

I want to filter out all rows that include negative numbers in them.

And, hopefully, get this:

JavaScript

I tried this so far:

JavaScript

But as you can see I get 1-dimensional array and I am getting unwanted rows (in this case is [2, 2])

How can I do this without using any for loop? Thanks in advance.

Advertisement

Answer

You can use np.all to check that all of the values in a row meet the condition.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement