2

I have an array of floats (size 9300), and another array of ints (size 2600) with indexes of the first array. I am trying to get the values in the first array based on the index in the second array. Example:

index   1st_array
0       12.00
1       3.89 
2       24.20
3       34.60

index  2nd_array
0       0
1       2

The output:

index      3nd_array
0          12.00
1          24.20

Edit: those are actually numpy arrays:

a1 = np.array([12.00, 3.89, 24.20, 34.60])
a2 = np.array([0, 2])

2 Answers 2

3
import numpy as np
arr1 = np.array([12.00, 3.89, 24.20, 34.60])
arr2 = np.array([0, 2])
print(arr1[arr2])

prints

[12.  24.2]
Sign up to request clarification or add additional context in comments.

Comments

1

If you have two Series s1 and s2, use:

s1.loc[s2]

Example:

s1 = pd.Series(['a', 'b', 'c', 'd'])
s2 = pd.Series([0, 2])

s1.loc[s2]
0    a
2    c
dtype: object

For numpy arrays

a1[a2]

Example:

a1 = np.array(['a', 'b', 'c', 'd'])
a2 = np.array([0, 2])

a1[a2]
array(['a', 'c'], dtype='<U1')

For DataFrames:

df1.set_index('index').loc[df2['2nd_array']].reset_index()

NB. the set_index/reset_index are only required if "index" are columns

Example:

df1 = pd.DataFrame({'index': [0, 1, 2, 3], 
                    '1st_array': [12.0, 3.89, 24.2, 34.6]})

df2 = pd.DataFrame({'index': [0, 1],
                    '2nd_array': [0, 2]})

(df1
 .set_index('index')
 .loc[df2['2nd_array']]
 .reset_index()
 )

   index  1st_array
0      0       12.0
1      2       24.2

2 Comments

@Fred so which case did you have?
For numpy arrays, but the one for dataframe end up being useful for something else as well.

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.