0
0
NumPydata~10 mins

Practical uses of structured arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Practical uses of structured arrays
Define structured dtype
Create structured array
Access fields by name
Perform operations on fields
Use structured array for analysis or export
We define a structured data type, create an array with named fields, access and manipulate these fields, then use the array for analysis or saving.
Execution Sample
NumPy
import numpy as np

# Define dtype
person_dtype = [('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]

# Create array
people = np.array([('Alice', 25, 55.0), ('Bob', 30, 85.5)], dtype=person_dtype)

# Access ages
ages = people['age']
This code creates a structured array of people with name, age, and weight, then extracts the ages.
Execution Table
StepActionCode/ExpressionResult/Output
1Define structured dtypeperson_dtype = [('name', 'U10'), ('age', 'i4'), ('weight', 'f4')][('name', '<U10'), ('age', '<i4'), ('weight', '<f4')]
2Create structured arraypeople = np.array([('Alice', 25, 55.0), ('Bob', 30, 85.5)], dtype=person_dtype)array([('Alice', 25, 55. ), ('Bob', 30, 85.5)], dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])
3Access 'age' fieldages = people['age']array([25, 30], dtype=int32)
4Calculate average ageavg_age = np.mean(ages)27.5
5Filter people older than 26older = people[people['age'] > 26]array([('Bob', 30, 85.5)], dtype=person_dtype)
6Add 5 to all weightspeople['weight'] += 5array([('Alice', 25, 60. ), ('Bob', 30, 90.5)], dtype=person_dtype)
7ExitNo more stepsEnd of example
💡 All steps completed to show creation, access, filtering, and modification of structured arrays.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
person_dtypeundefined[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')][('name', 'U10'), ('age', 'i4'), ('weight', 'f4')][('name', 'U10'), ('age', 'i4'), ('weight', 'f4')][('name', 'U10'), ('age', 'i4'), ('weight', 'f4')][('name', 'U10'), ('age', 'i4'), ('weight', 'f4')][('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]
peopleundefinedarray([('Alice', 25, 55. ), ('Bob', 30, 85.5)], dtype=person_dtype)array([('Alice', 25, 55. ), ('Bob', 30, 85.5)], dtype=person_dtype)array([('Alice', 25, 55. ), ('Bob', 30, 85.5)], dtype=person_dtype)array([('Alice', 25, 55. ), ('Bob', 30, 85.5)], dtype=person_dtype)array([('Alice', 25, 60. ), ('Bob', 30, 90.5)], dtype=person_dtype)array([('Alice', 25, 60. ), ('Bob', 30, 90.5)], dtype=person_dtype)
agesundefinedundefinedarray([25, 30], dtype=int32)array([25, 30], dtype=int32)array([25, 30], dtype=int32)array([25, 30], dtype=int32)array([25, 30], dtype=int32)
avg_ageundefinedundefinedundefined27.527.527.527.5
olderundefinedundefinedundefinedundefinedarray([('Bob', 30, 85.5)], dtype=person_dtype)array([('Bob', 30, 85.5)], dtype=person_dtype)array([('Bob', 30, 85.5)], dtype=person_dtype)
Key Moments - 3 Insights
Why do we access fields by name like people['age'] instead of by index?
Structured arrays store data with named fields, so accessing by name (people['age']) directly retrieves that column. Indexing like people[0] gives the whole first record, not a field.
What happens when we modify a field like people['weight'] += 5?
This operation updates the 'weight' field for all records in place, changing the stored values. The structured array keeps the data organized by fields, so modifying one field affects only that data.
How does filtering like people[people['age'] > 26] work?
The condition people['age'] > 26 creates a boolean mask array. Using it to index people returns only records where the condition is True, effectively filtering the array.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 3. What is the value of 'ages'?
Aarray([55.0, 85.5])
Barray([25, 30])
Carray(['Alice', 'Bob'])
Darray([('Alice', 25, 55.0), ('Bob', 30, 85.5)])
💡 Hint
Check the 'Result/Output' column at Step 3 in the execution table.
At which step does the 'weight' field get increased by 5 for all records?
AStep 6
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the action 'Add 5 to all weights' in the execution table.
If we changed the filter condition to people['age'] > 30, what would the 'older' array contain at Step 5?
AOnly 'Alice'
BOnly 'Bob'
CAn empty array
DAll records
💡 Hint
Refer to Step 5 filtering logic and consider ages 25 and 30.
Concept Snapshot
Structured arrays store data with named fields.
Define dtype with field names and types.
Create array with tuples matching dtype.
Access fields by name like arr['field'].
Filter and modify fields easily.
Useful for mixed-type tabular data.
Full Transcript
This lesson shows how to use numpy structured arrays practically. First, we define a structured data type with field names and types. Then, we create an array of records matching this type. We access fields by their names, like 'age', to get arrays of that data. We perform operations such as calculating averages, filtering records by conditions, and modifying fields in place. Structured arrays help organize mixed data types in one array, making analysis and manipulation straightforward.