0

suppose I have two array arr1 and arr2

arr1 = ["ABC","DEF","GHI","XYZ"]

arr2 = ["ABC","XYZ"]

I need the index of element of arr2 in arr1.

required result =[0,3]

I am able to do this using pandas but need an effective way of doing maybe by numpy.

A = pd.series(arr1)
B = pd.series(arr2)

B.map(lambda x: np.where(A==x)[0][0]).tolist()

1 Answer 1

1

Try np.where this way:

>>> np.where(np.isin(arr1, arr2))[0]
array([0, 3], dtype=int64)
>>> 

If you want a list:

>>> np.where(np.isin(arr1, arr2))[0].tolist()
[0, 3]
>>> 
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.