1

say we have

a = numpy.arange(25).reshape(5,5)

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

By going

numpy.where(a[1])

> array([0, 1, 2, 3, 4])

and then something like

a[1][numpy.where(a[1])]

> array([5, 6, 7, 8, 9])

I can select the horizontal rows of an array and the respective values, However how can I have a similar where condition to select only specific vertical columns

ie.

numpy.where(condition)

> array([1, 6, 11, 16, 21]) 

2 Answers 2

1

I'm not sure exactly if this is what you mean, but you can index columns using [:,column_number], where : stands for "all rows":

a[:,1][numpy.where(a[1])]

# array([ 1,  6, 11, 16, 21])

The above, however, is equivalent to simply a[:,1]:

>>> a[:,1]
array([ 1,  6, 11, 16, 21])
Sign up to request clarification or add additional context in comments.

2 Comments

perfect thanks, although whats does the comma , stand for?
Thats how you separate the "row" index and the "column" index. So [2,3] would select row 2, column 3
1

Have a look at this tutorial to learn how to apply slicing on numpy arrays (https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.indexing.html). As for your question, the answer is:

a[:,1]

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.