Python Query – Arrays

Advertisements

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'

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

Leave a ReplyCancel reply