0

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 ?

2

2 Answers 2

1

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})
Sign up to request clarification or add additional context in comments.

Comments

1

You can use np.where

print(student_data[np.where(student_data['score'] < 90)][['name', 'studentId']])

[('Alice', 1) ('Cathy', 3) ('Dorothy', 4)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.