Home > Software engineering >  How to get values in an array using another array with the indexes of the values?
How to get values in an array using another array with the indexes of the values?

Time:04-23

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])

CodePudding user response:

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]

CodePudding user response:

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
  • Related