Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get values in an array using another array with the indexes of the values?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading