Python Query – Arrays

Question:

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?

Asked By: IAwa

||

Answers:

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]])
Answered By: I'mahdi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.