0
0
NumPydata~5 mins

Practical uses of structured arrays in NumPy

Choose your learning style9 modes available
Introduction

Structured arrays help organize different types of data together in one table. This makes it easy to work with complex data like records or mixed information.

When you have data with different types, like names (text), ages (numbers), and scores (floats).
When you want to store and access data like a spreadsheet with columns of different types.
When you need to sort or filter data based on one or more fields.
When you want to perform calculations on specific parts of your data easily.
When you want to save and load complex data efficiently.
Syntax
NumPy
import numpy as np

# Define a structured array with fields
structured_array = np.array([
    ('Alice', 25, 88.5),
    ('Bob', 30, 92.0),
    ('Charlie', 22, 79.5)
], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')])

The dtype defines the fields: name (string), age (integer), score (float).

Each element is a tuple matching the fields in order.

Examples
This shows how to create an empty structured array with defined fields.
NumPy
import numpy as np

# Empty structured array with 3 fields
empty_array = np.array([], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')])
print(empty_array)
Structured array with a single record.
NumPy
import numpy as np

# Structured array with one element
one_element = np.array([('Diana', 28, 85.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')])
print(one_element)
Access the 'name' field from the structured array.
NumPy
import numpy as np

# Accessing a field
print(one_element['name'])
Sort the structured array by the 'age' field.
NumPy
import numpy as np

# Sorting by age
sorted_array = np.sort(one_element, order='age')
print(sorted_array)
Sample Program

This program creates a structured array of students with their names, ages, and scores. It shows how to access a single field, filter by age, and sort by score.

NumPy
import numpy as np

# Create a structured array with fields: name, age, score
students = np.array([
    ('Alice', 25, 88.5),
    ('Bob', 30, 92.0),
    ('Charlie', 22, 79.5),
    ('Diana', 28, 85.0)
], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')])

print("Original array:")
print(students)

# Access the 'age' field
ages = students['age']
print("\nAges:")
print(ages)

# Filter students older than 25
older_students = students[students['age'] > 25]
print("\nStudents older than 25:")
print(older_students)

# Sort students by score
sorted_by_score = np.sort(students, order='score')
print("\nStudents sorted by score:")
print(sorted_by_score)
OutputSuccess
Important Notes

Time complexity for accessing fields is O(1) because fields are stored separately.

Filtering and sorting depend on the number of elements, typically O(n) for filtering and O(n log n) for sorting.

Common mistake: forgetting to define the dtype properly, which causes errors or wrong data types.

Use structured arrays when you want to keep related data together but with different types, instead of separate arrays.

Summary

Structured arrays store mixed data types in one array with named fields.

They make it easy to access, filter, and sort complex data.

Useful for handling tabular data like records or datasets with different types.