Skip to main content

Introduction to Python NumPy Array Indexing

Python NumPy Array Indexing

Array indexing refers to accessing and retrieving specific elements, subsets, or slices of an array.

NumPy provides flexible and powerful indexing techniques that allow you to manipulate and extract data from arrays efficiently.

Here are some common methods for indexing NumPy arrays:

One-Dimensional Arrays

For a one-dimensional array, indexing works similar to Python lists. You can access individual elements by specifying the index within square brackets.

import numpy as np

# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])

# Access individual elements
print(arr[0]) # Access the first element
print(arr[2]) # Access the third element

# Access elements using negative indexing
print(arr[-1]) # Access the last element

Multi-Dimensional Arrays

For multi-dimensional arrays, you can use comma-separated indices or index tuples to access elements in different dimensions.

import numpy as np

# Create a 2-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Access individual elements
print(arr[0, 0]) # Access the element at (0, 0)
print(arr[1, 2]) # Access the element at (1, 2)

# Access elements in a row or column
print(arr[0]) # Access the first row
print(arr[:, 1]) # Access the second column

Slicing

NumPy supports slicing, which allows you to extract subsets of arrays based on specific ranges or patterns. Slicing is done using the colon : notation.

import numpy as np

# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])

# Slicing a portion of the array
print(arr[1:4]) # Slice elements from index 1 to 3 (exclusive)
print(arr[:3]) # Slice elements from the beginning to index 2 (exclusive)
print(arr[2:]) # Slice elements from index 2 to the end
print(arr[::2]) # Slice every other element

# Create a 2-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Slicing rows and columns
print(arr[1:3, :2]) # Slice rows 1 and 2, and columns 0 and 1

Negative Indexing

Negative indexing allows you to access elements of an array from the end, rather than the beginning.

It provides a convenient way to access elements relative to the end of the array without explicitly calculating the index.

Here's how negative indexing works in NumPy:

One-Dimensional Arrays

For a one-dimensional array, negative indexing allows you to access elements starting from the last element (-1) and counting backward.

import numpy as np

# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])

# Access elements using negative indexing
print(arr[-1]) # Access the last element
print(arr[-2]) # Access the second last element

Multi-Dimensional Arrays

For multi-dimensional arrays, you can use negative indexing on each dimension to access elements from the end in that dimension.

import numpy as np

# Create a 2-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Access elements using negative indexing
print(arr[-1, -1]) # Access the last element
print(arr[-2, -3]) # Access the second last element in the first row

You can apply negative indexing on each axis independently to access elements from the end in that specific axis.