Home > Mobile >  Python Query - Arrays
Python Query - Arrays

Time:11-20

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?

CodePudding user response:

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