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]])
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)
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...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.