0
$\begingroup$

I have the two following numpy arrays:

initial_array = np.array ([2, -10, 3, 5, 4])
indicator array = np.array([1, 3, 2, 3])

I want to generate a new numpy array which looks into the indicator array in order to extract values from the intial_array. The solution should look like this:

final_array =  [ 2  3  -10   3]
$\endgroup$

1 Answer 1

0
$\begingroup$

This is more of a programming than a data science question, but you can simply use the indicator_array to index the initial_array after subtracting 1 since arrays are zero indexed:

import numpy as np

initial_array = np.array ([2, -10, 3, 5, 4])
indicator_array = np.array([1, 3, 2, 3])

initial_array[indicator_array - 1]
# array([  2,   3, -10,   3])
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.