I'm sure the same or a similar question has been asked many times, but maybe my phrasing is off, so I didn't find a fitting post yet.
Here is an example:
import numpy as np
students = np.array(
[('Jim', 2), ('John', 3), ('Lisa', 5), ('Laura', 1)],
dtype=[('name', 'O'), ('grade', 'i4')]
)
# this works
good_students = students[students['grade'] < 3]
# this doesn't because unfortunately, numpy doesn't understand that I want
# it to do elementwise comparison
# absent_students = students[students['name'] in ['John', 'Laura']]
# this does but is ugly
absent_students = np.array(
[], dtype=[('name', 'O'), ('grade', 'i4')]
)
for name in ['John', 'Laura']:
absent_students = np.append(
absent_students, students[students['name'] == name]
)
Is there a better way than manually looping over the container?