A structured array stores compound records with named fields—like a lightweight typed table before reaching for Pandas.
Defining dtypes with fields
import numpy as np
dt = np.dtype([('name', 'U10'), ('score', 'f8')])
a = np.array([('Alice', 91.0), ('Bob', 87.5)], dtype=dt)
print(a['score'])
Field access
arr['field']— column-like access- Sort by field:
np.sort(arr, order='score') - Multiple fields in dtype for heterogeneous rows in one ndarray
When to use
Binary file formats, low-level sensor logs, or interop with C structs. For analytics, Pandas DataFrames are usually more ergonomic.
Important interview questions and answers
- Q: Structured vs object array?
A: Structured uses fixed layout per field—faster and typed; object stores PyObject pointers. - Q: Pandas equivalent?
A: DataFrame columns map to structured fields with richer indexing.
Self-check
- Define dtype with int id and float value fields.
- Access the score column from structured array.
Tip: For analytics tables, graduate to Pandas.
Interview prep
- Fields?
Named columns in one ndarray—like lightweight records.
- vs Pandas?
Pandas adds labels, IO, groupby—prefer for analytics.