NumPy Array Indexing

Introduction

NumPy array indexing is used to access individual elements from an array. We can select values using their index positions.

In this chapter, we will learn how to access elements from 1-D, 2-D and 3-D NumPy arrays.

What is Array Indexing?

Every element in a NumPy array has an index position. By default, indexing starts from 0.

For example, in the following array:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[0])
print(numbers[2])

Output:

10
30

Here, index 0 returns 10, and index 2 returns 30.

1. Accessing Elements in a 1-D Array

In a 1-D array, we can access an element by specifying its index inside square brackets [].

Example:

import numpy as np

marks = np.array([75, 80, 85, 90, 95])

print(marks[1])
print(marks[3])

Output:

80
90

2. Negative Indexing in NumPy

Negative indexing allows us to access array elements from the end.

  • -1 represents the last element.
  • -2 represents the second-last element.
  • -3 represents the third-last element.

Example:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[-1])
print(numbers[-2])

Output:

50
40

3. Accessing Elements in a 2-D Array

In a 2-D array, we use two index positions to access an element.

The first index represents the row, and the second index represents the column.

Syntax:

array[row_index, column_index]

Example:

import numpy as np

marks = np.array([[75, 80, 85],
                  [90, 95, 88]])

print(marks[0, 1])
print(marks[1, 2])

Output:

80
88

Here:

  • marks[0, 1] returns 80 from the first row and second column.
  • marks[1, 2] returns 88 from the second row and third column.

4. Negative Indexing in a 2-D Array

We can also use negative indexes to access elements from the end of rows and columns.

Example:

import numpy as np

marks = np.array([[75, 80, 85],
                  [90, 95, 88]])

print(marks[-1, -1])
print(marks[-2, -2])

Output:

88
80

Here, marks[-1, -1] returns the last element of the last row.

5. Accessing Elements in a 3-D Array

In a 3-D array, we use three index positions to access an individual element.

The indexes represent the array group, row and column.

Syntax:

array[group_index, row_index, column_index]

Example:

import numpy as np

data = np.array([
    [[10, 20], [30, 40]],
    [[50, 60], [70, 80]]
])

print(data[0, 1, 0])
print(data[1, 0, 1])

Output:

30
60

Here:

  • data[0, 1, 0] returns 30.
  • data[1, 0, 1] returns 60.

6. Accessing an Entire Row in a 2-D Array

We can use a single row index followed by a comma to access an entire row.

Example:

import numpy as np

sales = np.array([[1000, 1200, 1500],
                  [2000, 2200, 2500]])

print(sales[0])

Output:

[1000 1200 1500]

Here, sales[0] returns all elements from the first row.

7. Accessing an Entire Column in a 2-D Array

To access an entire column, use a colon : before the column index.

Example:

import numpy as np

sales = np.array([[1000, 1200, 1500],
                  [2000, 2200, 2500]])

print(sales[:, 1])

Output:

[1200 2200]

Here, : selects all rows, while 1 selects the second column.

Student Task: Practice Array Indexing

Use the following student marks array:

import numpy as np

marks = np.array([[75, 80, 85],
                  [90, 65, 70],
                  [88, 92, 78]])

Complete these tasks:

  1. Display the first element of the array.
  2. Access the marks 65.
  3. Display the last element using negative indexing.
  4. Access the element in the second row and third column.
  5. Display the complete first row.
  6. Display the complete second column.

Final Student Challenge: Analyze Monthly Sales

A company stores sales records for three products across two months.

import numpy as np

sales = np.array([
    [[1000, 1200], [1500, 1800]],
    [[2000, 2200], [2500, 2800]]
])

Write a Python program to:

  1. Display the first sales value.
  2. Access the value 1800.
  3. Display the last element using negative indexing.
  4. Access the value 2500.
  5. Display the first row of the first group.

Expected Output:

First Sales Value: 1000
Sales Value: 1800
Last Sales Value: 2800
Sales Value: 2500
First Row: [1000 1200]

Key Takeaways

  • NumPy indexing starts from 0.
  • Square brackets [] are used to access array elements.
  • Negative indexing accesses elements from the end.
  • 1-D arrays require one index.
  • 2-D arrays require row and column indexes.
  • 3-D arrays require three index positions.
  • A colon : can be used to select all rows or columns.

Frequently Asked Questions (FAQs)

1. What is NumPy Array Indexing in Python?

NumPy Array Indexing is used to access individual elements from a NumPy Array using their index positions. Indexing starts from 0.

2. How do you access elements in a NumPy Array?

You can access elements in a NumPy Array by specifying their index inside square brackets, such as arr[0] to access the first element.

3. What is the difference between Array Indexing and NumPy Array Indexing?

Both use index positions to access elements. However, NumPy Array Indexing also supports advanced features such as multidimensional indexing, slicing, and accessing multiple elements efficiently.

4. How does indexing work in a NumPy 2-D Array?

In a NumPy 2-D Array, you can access an element using two indexes: one for the row and another for the column. For example, arr[1, 2] accesses the element in the second row and third column.

5. Does NumPy Array Indexing start from 0 or 1?

NumPy Array Indexing starts from 0. This means the first element has index 0, the second has index 1, and so on.

6. How do you access the last element of a NumPy Array?

You can use negative indexing to access the last element of a NumPy Array. For example, arr[-1] returns the last element.

7. Can we use negative indexing in a NumPy Array?

Yes, NumPy supports negative indexing. It allows you to access elements from the end of an array. For example, arr[-2] returns the second-last element.

8. How do you access a specific row or column in a NumPy 2-D Array?

You can use indexing to access a specific row or column. For example, arr[1, :] accesses the second row, while arr[:, 1] accesses the second column of a NumPy 2-D Array.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top