0
0
NumPydata~5 mins

Creating structured arrays in NumPy

Choose your learning style9 modes available
Introduction

Structured arrays let you store different types of data together in one array. This helps when you want to keep related information organized, like a table with columns of different types.

You want to store data like a spreadsheet with columns of names, ages, and scores.
You need to handle data with mixed types, such as strings and numbers, in one array.
You want to access data by column names instead of just by position.
You want to save memory by using arrays instead of lists of dictionaries.
You want to perform fast operations on complex data with NumPy.
Syntax
NumPy
import numpy as np

# Define the data type with field names and types
dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')]

# Create an empty structured array
structured_array = np.zeros(3, dtype=dtype)

# Or create from a list of tuples
data = [('Alice', 25, 88.5), ('Bob', 30, 92.0), ('Cathy', 22, 79.5)]
structured_array = np.array(data, dtype=dtype)

The dtype defines the structure: each field has a name and a data type.

Use 'U10' for a string of max length 10, 'i4' for 4-byte integer, 'f4' for 4-byte float.

Examples
This creates an empty structured array with no rows.
NumPy
import numpy as np

dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')]

# Empty array with 0 rows
empty_array = np.zeros(0, dtype=dtype)
print(empty_array)
This creates a structured array with just one row.
NumPy
import numpy as np

dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')]

# Array with one element
one_element = np.array([('Diana', 28, 85.0)], dtype=dtype)
print(one_element)
You can get all values of a field by using the field name.
NumPy
import numpy as np

dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')]

# Accessing fields
students = np.array([('Eve', 21, 90.0), ('Frank', 24, 87.5)], dtype=dtype)
print(students['name'])
print(students['score'])
Sample Program

This program creates a structured array of students with name, age, and score. It prints the array, updates Bob's score, and prints the updated array.

NumPy
import numpy as np

# Define the structured data type
student_dtype = [('name', 'U10'), ('age', 'i4'), ('score', 'f4')]

# Create an array of students
students = np.array([
    ('Alice', 25, 88.5),
    ('Bob', 30, 92.0),
    ('Cathy', 22, 79.5)
], dtype=student_dtype)

print("Before update:")
print(students)

# Update Bob's score
for index, student in enumerate(students):
    if student['name'] == 'Bob':
        students[index]['score'] = 95.0

print("\nAfter update:")
print(students)
OutputSuccess
Important Notes

Time complexity for accessing or updating a field is O(n) if you loop, but O(1) if you use vectorized operations.

Space complexity is efficient because all data is stored in a single NumPy array.

Common mistake: forgetting to specify dtype or mismatching data types causes errors.

Use structured arrays when you want fast access by field names and compact storage, instead of lists of dictionaries.

Summary

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

You define the structure using a dtype with field names and types.

They help organize data like tables and allow easy access by column names.