Structured Arrays in NumPy

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn about structured arrays in NumPy. Structured arrays are ndarrays whose datatype is a composition of simpler datatypes organized as a sequence of named fields. They are useful for working with structured data, such as tabular data, where each field represents a different attribute of the data.

Note: You can write code in 07-structured-arrays.ipynb. Some printing operations are omitted in the steps, and you can print output as needed.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/comments -.-> lab-85704{{"`Structured Arrays in NumPy`"}} python/with_statement -.-> lab-85704{{"`Structured Arrays in NumPy`"}} python/variables_data_types -.-> lab-85704{{"`Structured Arrays in NumPy`"}} python/numeric_types -.-> lab-85704{{"`Structured Arrays in NumPy`"}} python/lists -.-> lab-85704{{"`Structured Arrays in NumPy`"}} python/tuples -.-> lab-85704{{"`Structured Arrays in NumPy`"}} python/importing_modules -.-> lab-85704{{"`Structured Arrays in NumPy`"}} python/numerical_computing -.-> lab-85704{{"`Structured Arrays in NumPy`"}} numpy/data_array -.-> lab-85704{{"`Structured Arrays in NumPy`"}} numpy/bool_idx -.-> lab-85704{{"`Structured Arrays in NumPy`"}} numpy/fancy_idx -.-> lab-85704{{"`Structured Arrays in NumPy`"}} end

Creating a Structured Array

To create a structured array, we can use the np.array function and specify the data type using the dtype parameter. The data type should be a list of tuples, where each tuple represents a field in the structured array. Each tuple should contain the field name and the data type of the field.

import numpy as np

## Create a structured array
x = np.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'U10'), ('age', int)])

Accessing Fields

We can access individual fields of a structured array by indexing with the field name. This will return a new array containing only the values of that field.

## Access the 'name' field
names = x['name']

Modifying Fields

We can also modify individual fields of a structured array by indexing with the field name and assigning new values.

## Modify the 'age' field
x['age'] = [26, 31]

Indexing with Multiple Fields

We can index a structured array with multiple fields by passing a list of field names. This will return a new structured array containing only the specified fields.

## Index with multiple fields
subset = x[['name', 'age']]

Comparing Structured Arrays

If the dtypes of two structured arrays are equal, we can compare them using the equality operator (==). This will return a boolean array indicating which elements have the same values for all fields.

## Compare two structured arrays
y = np.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'U10'), ('age', int)])
comparison = x == y

Creating a Record Array

A record array is a subclass of ndarray that allows access to fields by attribute instead of index. We can create a record array using the np.rec.array function.

## Create a record array
recordarr = np.rec.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'U10'), ('age', int)])

Accessing Fields by Attribute

We can access fields of a record array by attribute instead of index. This provides a more convenient way to work with structured data.

## Access fields by attribute
names = recordarr.name

Converting a Structured Array to a Record Array

We can convert a structured array to a record array using the view method and specifying the np.recarray type.

## Convert a structured array to a record array
recordarr = x.view(np.recarray)

Converting a Record Array to a Structured Array

To convert a record array back to a structured array, we can use the view method and specify the original dtype of the structured array.

## Convert a record array to a structured array
x = recordarr.view(dtype=[('name', 'U10'), ('age', int)])

Summary

In this lab, we learned how to create and work with structured arrays in NumPy. Structured arrays are useful for working with structured data, and they allow us to access and modify individual fields of the array. We also learned about record arrays, which provide a more convenient way to work with structured data by allowing access to fields by attribute instead of index.

Other Python Tutorials you may like