Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Multidimensional Indexing | Indexing and Slicing
course content

Course Content

Ultimate NumPy

Multidimensional IndexingMultidimensional Indexing

Now that you are able to access elements in 1D arrays, it’s time to learn about indexing in higher-dimensional arrays.

2D Arrays Indexing

Let's first take a look at a 2D array example:

2D Array

This is a 2x3 array, which means it consists of 2 1D arrays along axis 0, and each of these 1D arrays has 3 elements along axis 1.

The images below will clarify positive and negative indexing in 2D arrays:

2D Positive indexing
2D Negative indexing

As you can see, indexing along each of the axes is identical to indexing in 1D arrays.

Accessing Elements in 2D Arrays

In 1D arrays, we accessed elements by specifying the index of the element in square brackets. If we do the same in 2D arrays, we retrieve a 1D array at the specified index, which may be exactly what we need.

However, if we want to retrieve a particular element of an inner 1D array, we should specify the index of the 1D array (along axis 0) and the index of its element (along axis 1), e.g., array[0, 1]. We could also write array[0][1] as we do with Python list, but this is less efficient since it performs the search twice for each index instead of once.

Note

If a specified index is out of bounds, an IndexError is thrown, so be cautious of that.

Let's take a look at an example:

Task

stock_prices contains simulated stock prices over five days for five different companies. Each row corresponds to a particular company, and each column corresponds to a particular day. Consequently, each element in the matrix represents the closing price of a certain company's stock on a given day.

Your task is the following:

  1. Retrieve all the stock prices of the first company over five days with a positive index.
  2. Retrieve the stock price of the third company on the second day with positive indices.
  3. Retrieve the stock price of the last company on the last day with negative indices.

Everything was clear?

Section 2. Chapter 2
toggle bottom row
course content

Course Content

Ultimate NumPy

Multidimensional IndexingMultidimensional Indexing

Now that you are able to access elements in 1D arrays, it’s time to learn about indexing in higher-dimensional arrays.

2D Arrays Indexing

Let's first take a look at a 2D array example:

2D Array

This is a 2x3 array, which means it consists of 2 1D arrays along axis 0, and each of these 1D arrays has 3 elements along axis 1.

The images below will clarify positive and negative indexing in 2D arrays:

2D Positive indexing
2D Negative indexing

As you can see, indexing along each of the axes is identical to indexing in 1D arrays.

Accessing Elements in 2D Arrays

In 1D arrays, we accessed elements by specifying the index of the element in square brackets. If we do the same in 2D arrays, we retrieve a 1D array at the specified index, which may be exactly what we need.

However, if we want to retrieve a particular element of an inner 1D array, we should specify the index of the 1D array (along axis 0) and the index of its element (along axis 1), e.g., array[0, 1]. We could also write array[0][1] as we do with Python list, but this is less efficient since it performs the search twice for each index instead of once.

Note

If a specified index is out of bounds, an IndexError is thrown, so be cautious of that.

Let's take a look at an example:

Task

stock_prices contains simulated stock prices over five days for five different companies. Each row corresponds to a particular company, and each column corresponds to a particular day. Consequently, each element in the matrix represents the closing price of a certain company's stock on a given day.

Your task is the following:

  1. Retrieve all the stock prices of the first company over five days with a positive index.
  2. Retrieve the stock price of the third company on the second day with positive indices.
  3. Retrieve the stock price of the last company on the last day with negative indices.

Everything was clear?

Section 2. Chapter 2
toggle bottom row
some-alt