0

I have a numpy array similar to following structure:

my_array = numpy.array([[1,1,1,2,2,2,3,3,3],  
                  [1,2,3,1,2,3,1,2,3],
                  [1,1,32,4,15,63,763,23,0],
                  [1,1,2,3,1,2,3,1,1],
                  [1,1,1,1,1,1,1,1,1]])

Now I'd like to get subset this array to get only those columns where the value in the 3rd row is < 15.

I can get a boolean list of that as:

list(my_array[2,:]>15)

However I cannot use that boolean list for indexing like:

my_array[:,list(my_array[2,:]>15)]

Probably I have to transform that list to a list of indices and use that to subset the array, but maybe there is an in-built function or straight forward way for selecting the specific columns of the array?

1 Answer 1

1

You should not call list(). The input to [...] are supposed to be numpy arrays.

>>> my_array[:, my_array[2,:]>15]
array([[  1,   2,   3,   3],
       [  3,   3,   1,   2],
       [ 32,  63, 763,  23],
       [  2,   2,   3,   1],
       [  1,   1,   1,   1]])
Sign up to request clarification or add additional context in comments.

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.