{ "cells": [ { "cell_type": "markdown", "id": "56cfca86", "metadata": {}, "source": [ "# NumPy: Structured arrays" ] }, { "cell_type": "markdown", "id": "3b2e2335", "metadata": {}, "source": [ "## Introduction\n", "Structured arrays are ndarrays whose datatype is a composition of simpler datatypes organized as a sequence of named [`fields`](https://numpy.org/doc/stable/glossary.html#term-field)." ] }, { "cell_type": "markdown", "id": "91ef2a14", "metadata": {}, "source": [ "```python\n", ">>> x = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],\n", "... dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])\n", ">>> x\n", "array([('Rex', 9, 81.), ('Fido', 3, 27.)],\n", " dtype=[('name', '>> x[1]\n", "np.void(('Fido', 3, 27.0), dtype=[('name', '>> x['age']\n", "array([9, 3], dtype=int32)\n", ">>> x['age'] = 5\n", ">>> x\n", "array([('Rex', 5, 81.), ('Fido', 5, 27.)],\n", " dtype=[('name', '>> x['age'][x['name'] == 'Rex'] = 9\n", ">>> x\n", "array([('Rex', 9, 81.), ('Fido', 5, 27.)],\n", " dtype=[('name', '