Home > Back-end >  joint conditional truncation for ndarray
joint conditional truncation for ndarray

Time:11-18

Given a 2-dim numpy array

array([[-1.00, 1.00 ],
       [-2.00, 2.00 ],
       [ 2.00, 3.00 ],
       [ 1.00, 3.00 ],
       [-4.00,-3.00]])

I wish to retrieve the rows that both exceed a give value. That is, say all values larger or equal to one. This would be the desired output.

array([[ 2.00, 3.00 ],
       [ 1.00, 3.00 ]])

For a one dimensional array it can be done directly like

sample[sample >=1]

However, it doesn't work with an arrays of dim > 1. The syntax above also works for pandas dataframes but I don't want to convert everything to a dataframe in order to do the truncation.

Is there an elegant way to do that? Thx for your help.

CodePudding user response:

You can use np.array.all(axis=1):

>>> sample[(sample >= 1).all(axis=1)]
array([[2., 3.],
       [1., 3.]])
  • Related