0
0
NumPydata~5 mins

Accessing fields by name in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a structured array in NumPy?
A structured array is a NumPy array that can hold different data types in named fields, like columns in a table.
Click to reveal answer
beginner
How do you access a field named 'age' in a structured NumPy array called 'data'?
You use data['age'] to get all values in the 'age' field.
Click to reveal answer
beginner
Why is accessing fields by name useful in NumPy structured arrays?
It lets you work with specific columns easily without affecting other data, similar to selecting a column in a spreadsheet.
Click to reveal answer
intermediate
Can you assign new values to a field in a NumPy structured array? How?
Yes, by using data['field_name'] = new_values, you can update the values in that field.
Click to reveal answer
intermediate
What happens if you try to access a field name that does not exist in a NumPy structured array?
NumPy raises a ValueError because the field name is not found in the array's dtype.
Click to reveal answer
How do you access the 'height' field in a structured NumPy array named 'people'?
Apeople.get('height')
Bpeople['height']
Cpeople.height()
Dpeople.height
What type of object is returned when you access a field by name in a structured array?
AA NumPy array of the field's values
BA Python list
CA scalar value
DA dictionary
If 'data' is a structured array, what does data['age'] = 30 do?
ARaises an error
BDeletes the 'age' field
CAdds a new field 'age' with value 30
DSets all 'age' values to 30
What error occurs if you try to access a non-existent field in a structured array?
AValueError
BIndexError
CTypeError
DKeyError
Which of these is NOT a valid way to access fields in a NumPy structured array?
Aarray.field
Barray['field']
Carray.get('field')
Darray['field_name']
Explain how to access and update a field named 'score' in a NumPy structured array.
Think about how you select columns in a table.
You got /3 concepts.
    Describe what happens if you try to access a field that does not exist in a structured array.
    What error do you get when a key is missing in a dictionary?
    You got /3 concepts.