Use numpy to remove columns or rows that contain elements of certain conditions

Use numpy.where and numpy.delete in combination

>>> v = np.array(range(25)).reshape(5,5)
>>> v
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
>>> np.delete(v, np.where(v>22)[1], 1)
array([[ 0,  1,  2],
       [ 5,  6,  7],
       [10, 11, 12],
       [15, 16, 17],
       [20, 21, 22]])
>>> np.delete(v, np.where(v>18)[0], 1)
array([[ 0,  1,  2],
       [ 5,  6,  7],
       [10, 11, 12],
       [15, 16, 17],
       [20, 21, 22]])
>>> np.delete(v, np.where(v>18)[0], 0)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> 


Recommended Posts

Use numpy to remove columns or rows that contain elements of certain conditions
Apply functions to rows or columns of numpy.array without using list comprehensions
How to extract conditions (acquire all elements of Group that satisfy the conditions) for Group by Group
Convert elements of numpy array from float to int
How to use numpy
I want to judge the authenticity of the elements of numpy array
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.