What is Structured Arrays

Structured arrays in NumPy provide a way to work with structured or heterogeneous data where each element can have multiple fields with different data types. Structured arrays are similar to tables or structured data in databases, allowing you to organize and manipulate data of different types and sizes. Here’s an example:

import numpy as np

# Define the data types for the fields
dt = np.dtype([('name', 'S10'), ('age', int), ('salary', float)])

# Create a structured array
employees = np.array([('John', 30, 5000.0), ('Alice', 35, 6000.0)], dtype=dt)

print(employees)

Output:

[(b'John', 30, 5000.) (b'Alice', 35, 6000.)]

In this example, we define the data type dt for the structured array, specifying three fields: name as a string of maximum length 10 ('S10'), age as an integer (int), and salary as a float (float). We then create a structured array employees containing two records, each with values for the defined fields.

Structured arrays allow you to access and manipulate data using field names and indices. Here are some examples:

# Accessing individual fields
print("Names:", employees['name'])
print("Ages:", employees['age'])
print("Salaries:", employees['salary'])

# Accessing individual records
print("First record:", employees[0])
print("Second record:", employees[1])

# Accessing specific fields of a record
print("Name of first record:", employees[0]['name'])
print("Age of second record:", employees[1]['age'])

Output:

Names: [b'John' b'Alice']
Ages: [30 35]
Salaries: [5000. 6000.]
First record: (b'John', 30, 5000.)
Second record: (b'Alice', 35, 6000.)
Name of first record: b'John'
Age of second record: 35

In addition to accessing fields and records, structured arrays support various operations like slicing, sorting, and filtering based on conditions.

Structured arrays are useful for handling data that has multiple attributes or fields with different types. They provide a convenient way to organize, manipulate, and analyze structured or tabular data within NumPy arrays.

Leave a Reply

Your email address will not be published. Required fields are marked *