0
0
NumPydata~5 mins

Why structured arrays matter in NumPy

Choose your learning style9 modes available
Introduction

Structured arrays help you store different types of data together in one table. This makes it easy to work with complex data like names, ages, and scores all at once.

You have data with different types, like text and numbers, in one dataset.
You want to keep related information together, like a person's name and their age.
You need to perform calculations on some parts of the data but keep other parts as labels.
You want to save memory by using one array instead of many separate arrays.
You want to sort or filter data based on one or more fields.
Syntax
NumPy
import numpy as np

# Define a structured array with fields
person_dtype = np.dtype([('name', 'U10'), ('age', 'i4'), ('score', 'f4')])

# Create an array with this structure
people = np.array([('Alice', 25, 88.5), ('Bob', 30, 92.0)], dtype=person_dtype)

The dtype defines the names and types of each field in the array.

Each element in the array is like a small record with multiple pieces of data.

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

# Empty structured array
empty_people = np.array([], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')])
print(empty_people)
This example has just one record in the structured array.
NumPy
import numpy as np

# Structured array with one element
one_person = np.array([('Charlie', 22, 75.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')])
print(one_person)
You can get all values of one field easily by using the field name.
NumPy
import numpy as np

# Accessing fields
people = np.array([('Alice', 25, 88.5), ('Bob', 30, 92.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')])
print(people['name'])
print(people['score'])
You can sort the structured array by one of its fields, like age.
NumPy
import numpy as np

# Sorting by age
people = np.array([('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Charlie', 22, 75.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')])
sorted_people = np.sort(people, order='age')
print(sorted_people)
Sample Program

This program shows how to create a structured array, access one field, and sort the array by a field.

NumPy
import numpy as np

# Define the structured array type
person_dtype = np.dtype([('name', 'U10'), ('age', 'i4'), ('score', 'f4')])

# Create an array of people
people = np.array([
    ('Alice', 25, 88.5),
    ('Bob', 30, 92.0),
    ('Charlie', 22, 75.0)
], dtype=person_dtype)

print("Original array:")
print(people)

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

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

Structured arrays let you keep different data types together in one array.

Accessing fields by name is fast and easy.

Sorting by fields helps organize data for analysis.

Time complexity for sorting is O(n log n), where n is the number of records.

Space complexity is efficient because data is stored in one array.

A common mistake is forgetting to specify the dtype correctly, which can cause errors.

Use structured arrays when you want to keep related data together and work with it easily.

Summary

Structured arrays store multiple types of data in one place.

You can access and sort data by field names.

This makes working with complex data simpler and faster.