0

I need to access this numpy array, sometimes with only the rows where the last column is 0, and sometimes the rows where the value of the last column is 1.

y = [0  0  0  0
     1  2  1  1 
     2 -6  0  1
     3  4  1  0]

I have to do this over and over, but would prefer to shy away from creating duplicate arrays or having to recalculate each time. Is there someway that I can identify the indices concerned and just call them? So that I can do this:

>>print y[LAST_COLUMN_IS_0] 
[0  0  0  0
3  4  1  0]

>>print y[LAST_COLUMN_IS_1] 
[1  2  1  1 
2 -6  0  1]

P.S. The number of columns in the array never changes, it's always going to have 4 columns.

1 Answer 1

6

You can use numpy's boolean indexing to identify which rows you want to select, and numpy's fancy indexing/slicing to select the whole row.

print y[y[:,-1] == 0, :]
print y[y[:,-1] == 1, :]

You can save y[:,-1] == 0 and ... == 1 as usual, since they are just numpy arrays.

(The y[:,-1] selects the whole of the last column, and the == equality check happens element-wise, resulting in an array of booleans.)

Sign up to request clarification or add additional context in comments.

3 Comments

+1, It works. But I'm having trouble understanding what y[:,-1] == 0 is. It's a numpy array, but not a range? How can you then use it to index?
Thanks. Quick additional question, how do I then select a column from the result. For example, to select the 1st column, this doesn't work: y[y[:,-1] == 0, :][0]

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.