Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main advantage of using numpy structured arrays in data science?
easy
A. They allow storing different data types in one array with named fields.
B. They only store integers efficiently.
C. They automatically visualize data.
D. They replace all pandas functionality.

Solution

  1. Step 1: Understand structured arrays

    Structured arrays let you store mixed data types in one array with named fields, like columns in a table.
  2. Step 2: Compare options

    Only They allow storing different data types in one array with named fields. correctly describes this main advantage. Others are incorrect or unrelated.
  3. Final Answer:

    They allow storing different data types in one array with named fields. -> Option A
  4. Quick Check:

    Structured arrays = mixed types + named fields [OK]
Hint: Remember: structured arrays hold mixed types with names [OK]
Common Mistakes:
  • Thinking structured arrays only store one data type
  • Confusing structured arrays with visualization tools
  • Assuming structured arrays replace pandas completely
2. Which of the following is the correct way to define a structured array with fields 'name' (string) and 'age' (integer) in numpy?
easy
A. np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'f8'), ('age', 'S10')])
B. np.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'int'), ('age', 'float')])
C. np.array([(25, 'Alice'), (30, 'Bob')], dtype=[('age', 'i4'), ('name', 'S10')])
D. np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'S10'), ('age', 'i4')])

Solution

  1. Step 1: Check field names and types

    The fields are 'name' as string (bytes) and 'age' as integer. 'S10' means string of max 10 bytes, 'i4' means 4-byte integer.
  2. Step 2: Validate each option

    np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'S10'), ('age', 'i4')]) matches the correct dtype and data format. np.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'int'), ('age', 'float')]) uses wrong types. np.array([(25, 'Alice'), (30, 'Bob')], dtype=[('age', 'i4'), ('name', 'S10')]) swaps fields order and data. np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'f8'), ('age', 'S10')]) swaps types incorrectly.
  3. Final Answer:

    np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'S10'), ('age', 'i4')]) -> Option D
  4. Quick Check:

    Correct dtype and data order = np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'S10'), ('age', 'i4')]) [OK]
Hint: Match field names and types exactly in dtype [OK]
Common Mistakes:
  • Using wrong data types in dtype
  • Swapping field order and data
  • Not using byte strings for fixed-length strings
3. Given the structured array:
data = np.array([(b'Alice', 25), (b'Bob', 30), (b'Carol', 22)], dtype=[('name', 'S10'), ('age', 'i4')])
sorted_data = np.sort(data, order='age')

What is the output of sorted_data['name']?
medium
A. [b'Carol' b'Alice' b'Bob']
B. [b'Alice' b'Bob' b'Carol']
C. [b'Bob' b'Carol' b'Alice']
D. [b'Carol' b'Bob' b'Alice']

Solution

  1. Step 1: Understand sorting by 'age'

    The array is sorted by the 'age' field ascending: 22 (Carol), 25 (Alice), 30 (Bob).
  2. Step 2: Extract 'name' field after sorting

    After sorting, the 'name' field order matches sorted ages: Carol, Alice, Bob.
  3. Final Answer:

    [b'Carol' b'Alice' b'Bob'] -> Option A
  4. Quick Check:

    Sort by age ascending = Carol, Alice, Bob [OK]
Hint: Sort by field then check that field's order [OK]
Common Mistakes:
  • Assuming original order remains after sort
  • Mixing up ascending vs descending order
  • Confusing field names when accessing
4. Consider this code snippet:
data = np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'S10'), ('age', 'i4')])
filtered = data[data['age'] > 25]

What is the error in this code?
medium
A. ValueError because comparison with > is invalid on structured arrays.
B. No error; it correctly filters entries with age > 25.
C. TypeError because 'age' field is not accessible.
D. SyntaxError due to wrong indexing syntax.

Solution

  1. Step 1: Check filtering syntax

    Filtering structured arrays by a field with a condition like data['age'] > 25 is valid and returns a boolean mask.
  2. Step 2: Confirm no errors

    The code correctly filters rows where age is greater than 25, so no error occurs.
  3. Final Answer:

    No error; it correctly filters entries with age > 25. -> Option B
  4. Quick Check:

    Filtering with boolean mask on field works [OK]
Hint: Use boolean masks on fields to filter structured arrays [OK]
Common Mistakes:
  • Thinking structured arrays can't be filtered by fields
  • Confusing syntax for filtering
  • Assuming comparison operators don't work on fields
5. You have a structured array of employees with fields 'name' (string), 'age' (int), and 'salary' (float). You want to find the average salary of employees older than 30. Which code snippet correctly does this?
hard
A. avg_salary = data[data['salary'] > 30]['age'].mean()
B. avg_salary = np.mean(data['salary'] > 30)
C. avg_salary = data['salary'][data['age'] > 30].mean()
D. avg_salary = data['salary'].mean(data['age'] > 30)

Solution

  1. Step 1: Filter employees older than 30

    Use boolean mask data['age'] > 30 to select salaries of employees older than 30.
  2. Step 2: Calculate mean salary of filtered data

    Apply .mean() on the filtered salary array to get average salary.
  3. Final Answer:

    avg_salary = data['salary'][data['age'] > 30].mean() -> Option C
  4. Quick Check:

    Filter by age, then mean salary = avg_salary = data['salary'][data['age'] > 30].mean() [OK]
Hint: Filter first, then compute mean on selected field [OK]
Common Mistakes:
  • Using mean on boolean arrays instead of salaries
  • Mixing up fields in filtering and aggregation
  • Passing filter as argument to mean() incorrectly