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

Python Query – Arrays

Create two arrays using numpy. One called students with as values.

['Janet', 'Adriana', 'Manual', 'Mohamed', 'Leann']

Another is called grades as values:

[[93, 85], [78, 80], [94, 93], [75, 90], [92, 87]]

Select all rows from grades where student is either 'Adriana' or 'Mohamed'

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

How do i go about this problem?

>Solution :

You can use numpy.isin.

import numpy as np
students = ['Janet', 'Adriana', 'Manual', 'Mohamed', 'Leann']
grades = [[93, 85], [78, 80], [94, 93], [75, 90], [92, 87]]
arr_s = np.asarray(students)
arr_g = np.asarray(grades)
mask = np.isin(arr_s, ['Adriana', 'Mohamed'])
res = arr_g[mask]
print(res)

Output:

array([[78, 80],
       [75, 90]])
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