NumPy Structured Arrays
In real-world applications, data is not always uniform.
For example, a dataset may contain:
- Names (string)
- Age (integer)
- Salary (float)
NumPy structured arrays allow you to store multiple data types in one array.
What are Structured Arrays?
A structured array is a NumPy array where each element can have different data types (fields).
Each field works like a column in a table.
Why Use Structured Arrays?
- Store mixed data types
- Similar to database tables
- Easy data organization
- Useful for data analysis
- Efficient memory usage
1. Creating a Structured Array
import numpy as np
data = np.array([
("Alice", 25, 50000.0),
("Bob", 30, 60000.0),
("Charlie", 28, 55000.0)
], dtype=[("name", "U10"), ("age", "i4"), ("salary", "f4")])
print(data)
Output
[('Alice', 25, 50000.)
('Bob', 30, 60000.)
('Charlie', 28, 55000.)]
2. Accessing Fields
You can access data like columns.
print(data["name"])
print(data["age"])
Output
['Alice' 'Bob' 'Charlie']
[25 30 28]
3. Accessing Single Record
print(data[0])
Output
('Alice', 25, 50000.)
4. Sorting Structured Arrays
You can sort by any field.
sorted_data = np.sort(data, order="age")
print(sorted_data)
Output
[('Alice', 25, 50000.)
('Charlie', 28, 55000.)
('Bob', 30, 60000.)]
5. Sorting by Salary
sorted_data = np.sort(data, order="salary")
print(sorted_data)
Output
[('Alice', 25, 50000.)
('Charlie', 28, 55000.)
('Bob', 30, 60000.)]
6. Filtering Data
filtered = data[data["age"] > 27]
print(filtered)
Output
[('Bob', 30, 60000.)
('Charlie', 28, 55000.)]
7. Updating Values
data["salary"][0] = 52000
print(data)
Output
[('Alice', 25, 52000.)
('Bob', 30, 60000.)
('Charlie', 28, 55000.)]
8. Adding New Data
Structured arrays are fixed size, so we use append.
new_data = np.append(data, [("David", 35, 70000.0)])
print(new_data)
9. Real-World Example: Employee Records
employees = np.array([
("John", 40, 80000),
("Emma", 29, 65000),
("Mike", 33, 72000)
], dtype=[("name", "U10"), ("age", "i4"), ("salary", "i4")])
print(employees)
10. Real-World Example: Student Database
students = np.array([
("A", 90),
("B", 75),
("C", 88)
], dtype=[("name", "U10"), ("marks", "i4")])
print(students)
Structured Array Field Types
| Type | Description |
|---|---|
| U10 | String (Unicode) |
| i4 | Integer |
| f4 | Float |
Advantages of Structured Arrays
- Store mixed data types
- Easy column-based access
- Useful for tabular data
- Efficient memory usage
- Good for data analysis
Summary
NumPy structured arrays allow you to store and manage heterogeneous data in a single array. They behave like lightweight database tables and are highly useful in data science workflows.
This feature is part of NumPy and widely used in applications built with Python.
Conclusion
Structured arrays are powerful tools for handling real-world datasets containing multiple data types. They simplify data storage, access, and processing in Python.


0 Comments