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

Conteúdo do Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

bookMultidimensional 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:

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:

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:

12345678910
import numpy as np array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Accessing the first element (1D array) with positive index print(array_2d[0]) print('-' * 7) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) print('-' * 7) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
copy
Tarefa
test

Swipe to show code editor

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
toggle bottom row

bookMultidimensional 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:

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:

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:

12345678910
import numpy as np array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Accessing the first element (1D array) with positive index print(array_2d[0]) print('-' * 7) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) print('-' * 7) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
copy
Tarefa
test

Swipe to show code editor

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
toggle bottom row

bookMultidimensional 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:

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:

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:

12345678910
import numpy as np array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Accessing the first element (1D array) with positive index print(array_2d[0]) print('-' * 7) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) print('-' * 7) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
copy
Tarefa
test

Swipe to show code editor

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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:

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:

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:

12345678910
import numpy as np array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Accessing the first element (1D array) with positive index print(array_2d[0]) print('-' * 7) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) print('-' * 7) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
copy
Tarefa
test

Swipe to show code editor

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt