0
0
NumPydata~10 mins

Accessing fields by name in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Accessing fields by name
Create structured array with named fields
Access field by name using array['field_name'
Retrieve data for that field
Use or display the extracted data
We create a structured array with named fields, then access data by specifying the field name in brackets, retrieving that field's values.
Execution Sample
NumPy
import numpy as np
arr = np.array([(1, 2.0), (3, 4.0)], dtype=[('x', 'i4'), ('y', 'f4')])
print(arr['x'])
This code creates a structured array with fields 'x' and 'y', then prints the values in field 'x'.
Execution Table
StepActionCode/ExpressionResult/Value
1Create structured arraynp.array([(1, 2.0), (3, 4.0)], dtype=[('x', 'i4'), ('y', 'f4')])array([(1, 2.), (3, 4.)], dtype=[('x', '<i4'), ('y', '<f4')])
2Access field 'x'arr['x']array([1, 3], dtype=int32)
3Print field 'x'print(arr['x'])[1 3]
4Access field 'y'arr['y']array([2., 4.], dtype=float32)
5Print field 'y'print(arr['y'])[2. 4.]
6ExitNo more actionsEnd of execution
💡 All fields accessed and printed, execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
arrundefinedstructured array with fields 'x' and 'y'same arraysame arraysame array
arr['x']undefinedundefinedarray([1, 3])array([1, 3])array([1, 3])
arr['y']undefinedundefinedundefinedarray([2., 4.])array([2., 4.])
Key Moments - 2 Insights
Why do we use arr['x'] instead of arr.x to access the field?
In numpy structured arrays, fields are accessed by indexing with the field name as a string, like arr['x'], because arr.x syntax is not supported. See execution_table step 2 where arr['x'] retrieves the field.
What type of data is returned when accessing a field by name?
Accessing a field returns a numpy array containing all values for that field across records, as shown in execution_table step 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of arr['x'] at step 2?
Aarray([(1, 2.), (3, 4.)])
Barray([2., 4.])
Carray([1, 3])
Darray([3, 1])
💡 Hint
Check the 'Result/Value' column at step 2 in the execution_table.
At which step do we first print the values of field 'y'?
AStep 3
BStep 5
CStep 2
DStep 4
💡 Hint
Look for 'Print field 'y'' in the Action column of execution_table.
If we tried to access arr['z'], what would happen?
ARaise an IndexError
BReturn all fields
CReturn an empty array
DReturn None
💡 Hint
Numpy structured arrays only allow access to defined field names as shown in variable_tracker.
Concept Snapshot
Accessing fields by name in numpy structured arrays:
- Use arr['field_name'] to get all values of that field.
- Returns a numpy array of that field's data.
- arr.field_name syntax is not supported.
- Useful for working with tabular data with named columns.
Full Transcript
This lesson shows how to access fields by name in numpy structured arrays. We create an array with named fields 'x' and 'y'. Then we access the field 'x' by using arr['x'], which returns all values in that field as a numpy array. We print these values to see the output. Similarly, we access and print the field 'y'. This method is the standard way to get data from named fields in numpy structured arrays. The key point is to use square brackets with the field name as a string. Trying to use dot notation will not work. Accessing a field returns an array of values for that field across all records. This is useful for selecting columns from structured data.