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.
Why structured arrays matter in 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.
import numpy as np # Empty structured array empty_people = np.array([], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'f4')]) print(empty_people)
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)
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'])
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)
This program shows how to create a structured array, access one field, and sort the array by a field.
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)
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.
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.