0
0
NumPydata~10 mins

Record arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Record arrays
Define dtype with field names and types
Create record array with structured data
Access fields by name
Perform operations on fields
Use record array for analysis or display
Record arrays store data with named fields, like a table with columns. You define the structure, create the array, then access fields by name for easy analysis.
Execution Sample
NumPy
import numpy as np

dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')]
data = [('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)]
rec_arr = np.array(data, dtype=dtype)
print(rec_arr['age'])
Creates a record array with fields name, age, score and prints the age column.
Execution Table
StepActionData StateField AccessOutput
1Define dtype with fields[('name', 'U10'), ('age', 'i4'), ('score', 'f4')]N/AN/A
2Create record array with data[('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)]N/AN/A
3Access 'age' fieldRecord array unchangedrec_arr['age'][25 30 22]
4Access 'name' fieldRecord array unchangedrec_arr['name']['Alice' 'Bob' 'Cathy']
5Access 'score' fieldRecord array unchangedrec_arr['score'][88.5 92. 79.5]
6Filter ages > 23Record array unchangedrec_arr['age'] > 23[ True True False]
7Select records with age > 23Record array unchangedrec_arr[rec_arr['age'] > 23][('Alice', 25, 88.5) ('Bob', 30, 92.0)]
8ExitEnd of operationsN/AN/A
💡 All steps executed; final filtered record array shown.
Variable Tracker
VariableStartAfter Step 2After Step 7Final
dtypeundefined[('name', 'U10'), ('age', 'i4'), ('score', 'f4')][('name', 'U10'), ('age', 'i4'), ('score', 'f4')][('name', 'U10'), ('age', 'i4'), ('score', 'f4')]
dataundefined[('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)][('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)][('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)]
rec_arrundefined[('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)][('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)][('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)]
Key Moments - 3 Insights
Why can we access fields by name like rec_arr['age']?
Because the record array is created with a dtype that defines named fields, numpy allows direct access to each field as if it were a column. See execution_table step 3.
What happens when we filter with rec_arr['age'] > 23?
This creates a boolean array showing which records meet the condition. We then use it to select only those records. See execution_table steps 6 and 7.
Is the original record array changed after filtering?
No, filtering returns a new array with selected records. The original record array stays the same. See execution_table step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of rec_arr['score'] at step 5?
A[88.5 92. 79.5]
B[25 30 22]
C['Alice' 'Bob' 'Cathy']
D[True True False]
💡 Hint
Check the 'Output' column in execution_table row 5.
At which step does the condition rec_arr['age'] > 23 become False for some records?
AStep 3
BStep 7
CStep 6
DStep 4
💡 Hint
Look at the 'Field Access' and 'Output' columns in execution_table row 6.
If we change the dtype to remove the 'score' field, what happens when accessing rec_arr['score']?
AIt returns an empty array.
BIt raises an IndexError.
CIt returns all zeros.
DIt returns the 'age' field instead.
💡 Hint
Accessing a non-existent field in a record array causes an error.
Concept Snapshot
Record arrays store data with named fields.
Define dtype with field names and types.
Create array with structured data.
Access fields by name like columns.
Filter and select records easily.
Useful for table-like data in numpy.
Full Transcript
Record arrays in numpy let you store data with named fields, like columns in a table. First, you define a dtype that lists each field's name and data type. Then you create a numpy array with this dtype and your data. You can access each field by its name, for example rec_arr['age'] gives all ages. You can also filter records by conditions on fields, like selecting only those with age greater than 23. The original array stays unchanged when filtering; a new array is returned. This makes record arrays very handy for structured data analysis.