Indexing Structured Arrays in NumPy

Learn via video courses
Topics Covered

Overview

NumPy offers us a lot of different functionalities when it comes to array manipulation. We can create dictionary-type arrays in NumPy, which store multiple values in the same array. By doing this, we can search and retrieve elements in no time. These arrays are called structured arrays.

Introduction

Before diving into structured arrays, let us understand indexing. Indexing is done when we have a lot of data-value pairs, and we want to arrange them in such a way that every field has its index. By using this way, we can retrieve fields in a quicker sense.

structure-array-example

As you can see, we have indexed our fields as 0, 1, and 2. If we want to retrieve the number of items for the Apple iPhone X, we can simply call the row with index 1.

Indexing in Structured Arrays

A structured array is a way to group several related variables/values into a single array. These types of arrays can hold many different types of data types, like int, float, boolean, character, etc.

To create a structured array, we need to call the dtype() constructor and pass a list of our desired tuples in there. The name of the field and the appropriate data type must be included in each tuple.

Here's an example of a structured array in NumPy:

Output

Here's another example in which we will create a structured NumPy array of people with their age and weight:

Output

Accessing Individual Fields

A field in NumPy is like a data container; it can contain data of any type and size. By accessing the value of a field, we can retrieve the whole data that is linked with the respective field. Let's create a structured array to demonstrate accessing individual fields:

Output

In this example, our fields are productName, numberOfQuantity, price and inStock. Now, we will try and access the field-specific data from our structured array.

Output

If we were to access the inStock field:

Output

Hence, we got our boolean field inStock as a NumPy array.

Accessing Multiple Fields

We can assign an index to a multi-field structured array in NumPy, where the indexes are a list of field names, respectively. When we access multiple fields at the same time, we should note that the order won't be changed.

Output

Indexing with an Integer to Get a Structured Scalar

Just as we use field names to retrieve information out of our structured arrays, we can use indexes too. We can think of a structured array as a two-dimensional matrix; having rows and columns.

indexing-structured-scalar

As you can see, we can retrieve information stored in specific cells of the two-dimensional matrix. Let's say that we want to get the data stored in the (1, 3) cell. We can simply get it by using the arr[1][3] notation. Similarly, we will use indexing to get information out of our structured array:

We will imagine this structured array as a two-dimensional matrix:

indexing-structured-scalar2

Output

Similarly, if we were to retrieve the name of the mobile from the first row and the first column, we would use double indexing:

Output

Record Arrays in NumPy

Record arrays in NumPy are quite similar to structured arrays. The only difference is that it only allows the user to access the array's fields by attributes, and not by indexes. To achieve this, record arrays use a special type of datatype called numpy.record, which allows NumPy to access fields by using attributes only.

Output

By using slice indexing (1:2), we can access the elements of our record arrays. In slice indexing, the first parameter is the inclusive index, whereas the second parameter is non-inclusive. 1:2 means that we'll take everything that includes 1 and is between 1 and 2 (2 is exclusive).

Another property of record arrays is that the record returned by it is always in the form of a record array.

Output

Conclusion

  • In this article, we learned the basics of structured and record arrays in NumPy.
  • Structured arrays are based on the principles of structures in low-level programming languages like C; the way of grouping alike variables in a single array.
  • We also looked at different ways in which we could access the values of structured arrays using field names and index notations, such as accessing individual fields and multiple fields and using an integer as indexing to get a structured scalar.
  • We went through record arrays; which are structured arrays in which the user can access the fields by attributes only.