7

From the following array :

test = np.array([[1,2,'a'],[4,5,6],[7,'a',9],[10,11,12]])

How can I delete the rows that contain 'a' ? Expected result :

array([[ 4,  5,  6],
   [10, 11, 12]])

3 Answers 3

11

Note, numpy supports vectorized comparisons:

>>> test
array([[1, 2, 'a'],
       [4, 5, 6],
       [7, 'a', 9],
       [10, 11, 12]], dtype=object)
>>> test == 'a'
array([[False, False,  True],
       [False, False, False],
       [False,  True, False],
       [False, False, False]], dtype=bool)

Now, you want the rows where all are not equalt to 'a':

>>> (test != 'a').all(axis=1)
array([False,  True, False,  True], dtype=bool)

So, simply select the rows with the mask:

>>> row_mask = (test != 'a').all(axis=1)
>>> test[row_mask,:]
array([[4, 5, 6],
       [10, 11, 12]], dtype=object)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your great answer. I was trying to find a solution with the np.where function but I did not. Thanks again.
@GF you could use np.where, but I think the solution would be less clean, unless you were looking specifically for the values where something was whatever. The problem when you want only the rows or columns is that you have to do an additional step to find unique indices...
3

Also, like this maybe? (Inspired from one of my another answers )

In [100]: mask = ~(test == 'a')

In [101]: mask
Out[101]: 
array([[ True,  True, False],
       [ True,  True,  True],
       [ True, False,  True],
       [ True,  True,  True]], dtype=bool)

In [102]: test[np.all(mask, axis=1), :]
Out[102]: 
array([['4', '5', '6'],
       ['10', '11', '12']],
      dtype='<U21')

But, please note that here we're not deleting any rows from the original array. We're just slicing out the rows which doesn't have the alphabet a.

1 Comment

Nice too. Thanks ;)
2

To sum up, there are a few possible ways such as :

test[np.all(test != 'a', axis=1), :]

Or

test[(test != 'a').all(axis=1)]

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.