Reputation: 2085
I am new to numpy and still learning it. I created a structured array as below:
name = ['Alice', 'Beth', 'Cathy', 'Dorothy']
studentid = [1,2,3,4]
score = [85.4, 90.4, 87.66, 78.9]
student_data = np.zeros(4, dtype={'names':('name', 'studentId', 'score'),
'formats':('U10', 'i4', 'f8')})
student_data['name'] = name
student_data['studentId'] = studentid
student_data['score']=score
Inorder to get the names of people who have scores greater than 85, I wrote this:
student_data[student_data['score'] > 85]['name']
But if I try to retrieve another column along with 'name', I am getting an error:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
For example, I tried the below ways:
student_data[student_data['score'] < 90]['name','studentid']
student_data[student_data['score'] < 90]['name']['studentid']
Both of them result in the error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-189-7ad7c151f0e3> in <module>
----> 1 student_data[student_data['score'] < 90]['name']['studentid']
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Could anyone let me know what is the mistake I am doing here & how can I retrieve multiple columns on conditional basis ?
Upvotes: 0
Views: 164
Reputation: 8302
You can use np.where
print(student_data[np.where(student_data['score'] < 90)][['name', 'studentId']])
[('Alice', 1) ('Cathy', 3) ('Dorothy', 4)]
Upvotes: 1
Reputation: 150735
You can access with array of columns:
student_data[student_data['score'] < 90][['name','studentid']]
Output:
array([('Alice', 1), ('Cathy', 3), ('Dorothy', 4)],
dtype={'names':['name','studentId'], 'formats':['<U10','<i4'], 'offsets':[0,40], 'itemsize':52})
Upvotes: 1