1

I have a numpy.ndarray and I have a boolean list. I want to use the list to access the columns in the array.

X = [[1,2,3,4],[5,6,7,8]]
Y = [True,False,False,True]

I want the result to be

[[1,4][5,8]]

I guess I am doing this inefficiently and would like to know if there is a straightforward method.

4
  • 1
    ..... X[:,Y]? Commented Oct 26, 2017 at 16:43
  • X[:,[0,3]]?.. Commented Oct 26, 2017 at 16:45
  • Nope doesn't work. It says too many indices for array. Commented Oct 26, 2017 at 16:46
  • @Nivi, Divakar's answer should work, unless your sample input does not reflect your actual data's characteristics. I'd advise you to revisit your question. Commented Oct 26, 2017 at 16:50

1 Answer 1

2

You have to convert it to numpy first.

import numpy as np

X = np.array([[1,2,3,4],[5,6,7,8]])
Y = np.array([True,False,False,True])

print(X[:,Y])
Sign up to request clarification or add additional context in comments.

4 Comments

did you convert your array into numpy?
I did the copy/paste, and it works just as you'd like.
Yes it does. My y = array([ True, False, True, False], dtype=bool) Will check. Thanks.
@ it should be np.array, not just array. Can you please put your actual code in the question? if it is still not working.

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.