0

How can I extract values in Col1 whose Col0 matches any values in a numpy array. I have an np array A, idx. Get me all values in Col1 of array A, whose Col0 values are 1 or 4.

A = np.array([[1, 11], [2, 12], [3, 13], [4,14]])

idx = [1, 4]

I can get for 1 value like this.. but I don't know to get for an array of idx.

vals = A[np.where(A[:,0]==4),1]
vals = A[np.where(A[:,0]==4),4]

a) how can I get the values of Col1 in A where Col0 values are 1 or 4 ( matching idx).

expected result = [11,14]

b) how can I get values of Col1 in A where row indices are 1,4 (matching idx)

expected result = [12, 14]

1 Answer 1

1

1st part:

idx = [1, 4]
A[np.isin(A[:,0], idx), 1]

array([11, 14])

2nd part:

idx = [1, 3]
A[idx,1]

array([12, 14])
Sign up to request clarification or add additional context in comments.

2 Comments

For the part2 I figured A[idx][:,1] would work. Thanks @Pygirl]
@py_newbie You have given wrong index. for 2nd part.

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.