Python NumPy Array Indexing

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 28, 2023
  • Reading time:13 mins read

Python NumPy array indexing is used to access values in the 1-dimensional and, multi-dimensional arrays. Indexing is an operation, that uses this feature to get a selected set of values from a NumPy array. Note that the index in ndarray starts from zero hence, there is a difference between the value and where the value is located in an array. In this article, I will explain how to get the values from an array by using its indexes.

1. Quick Examples of NumPy Array Indexing

If you are in a hurry, below are some quick examples of how to get Python NumPy array indexing.


# Quick examples of numpy array indexing

# Example 1: Use NumPy.array() 
# To get first value
arr = np.array([2, 4, 6, 8])
arr2 = arr[0]

# Example 2: Use NumPy.array() 
# To get third and fourth elements
arr = np.array([2, 4, 6, 8])
arr2 = arr[2] + arr[3]

# Example 3: Use array Indexing 
# To get the values of two-dimensional arrays
arr = np.array([[0,3,5,7,9], [11,13,15,17,19]])
arr2 = arr[0, 2]

# Example 4: Use array Indexing 
# To get the values of 2-D arrays
arr = np.array([[0,3,5,7,9], [11,13,15,17,19]])
arr2 = arr[1, 3]

# Example 5: Access values of 3-dimensional arrays using index
arr = np.array([[[0,3,5,], [7,9,11]], [[13, 15, 17], [19, 21, 23]]])
arr2 = arr[0, 1, 2]

# Example 6: Use negative array index
arr = np.array([[0,3,5,7,9],[11,13,15,17,19]])
arr2 = arr[0, -1]

# Example 7: Access the last element of the last row 
# Using negative indexing
arr = np.array([[0,3,5,7,9],[11,13,15,17,19]])
arr2 = arr[1, -1]

2. Get the 1-Dimensional NumPy Array Values Using Indexing

ndarrays is indexed using the standard Python x[obj] syntax, where x is the array and obj is the selection. You can access an array value by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, the second has index 1, etc.

In one-dimensional arrays, values are stored individually, so we can access those values by using their indices. Because every value in arrays has its own index. Another way to access elements from NumPy array using NumPy array slicing.

If you have a 1-dimensional NumPy array and you want to access its values using indexing. This code demonstrates the creation of a NumPy array, prints the original array, and then extracts and prints the first value of the array. In this case, the original array is [2, 4, 6, 8], and the first value (at index 0) is 2.


# Import numpy module
import numpy as np

# Create input array
arr = np.array([2, 4, 6, 8])
print("Original array:",arr)

# Use NumPy.array() 
# To get first value
arr2 = arr[0]
print("Getting first value:",arr2)

Yields below output.

NumPy array indexing

From the above, arr contains four values 24,6,and 8. Each of these values has its different indices.

Alternatively, you are using NumPy array indexing to get the third and fourth elements of the array arr and then add them together. In this code, you are accessing the elements at indices 2 and 3 of the array arr using indexing (arr[2] and arr[3]), and then you are adding these two elements together and storing the result in the variable arr2. Finally, you print the result.


# Create input array
arr = np.array([2, 4, 6, 8])
print("Original array:",arr)

# Use NumPy.array() 
# To get third and fourth elements
arr2 = arr[2] + arr[3]
print("Sum of third and fourth elements:",arr2)

Yields below output.

NumPy array indexing

3. Get the 2-Dimensional Array Values Using Indexing

Use array indexing to access 2-dimensional array values, you can use comma-separated integers representing the dimension and the index of the element. Two-dimensional arrays are like a table with rows and columns, where the row represents the dimension and the index represents the column.

You can use array indexing to get the value at the 3rd position (index 2) of the 1st row (index 0) of a 2-dimensional array. Using indexing (arr[0, 2]) to access the element at the 3rd position in the 1st row.


# Create 2D input array
arr = np.array([[0,3,5,7,9], [11,13,15,17,19]])

# Use array Indexing 
# To get the values of two-dimensional arrays
arr2 = arr[0, 2]
print("3rd value on 1st row:",arr2)

# Output:
# 3rd value on 1st row:  5

Similarly, using NumPy array indexing to get the value at the 4th element (index 3) of the 2nd row (index 1) of a 2-dimensional NumPy array. You are using indexing (arr[1, 3]) to access the element at the 4th position in the 2nd row.


# Use array Indexing 
# To get the values of 2-D arrays
arr2 = arr[1, 3]
print("4th element on 2nd row:",arr2)

# Output:
# 4th element on 2nd row: 17

4. Get the 3-Dimensional Array Values Using Array Indexing

Use NumPy array indexing to access 3-dimensional array values, you can use comma-separated integers representing the dimension and the index of the element.

If you have a 3-dimensional NumPy array and you want to access its values using array indexing. You can access individual elements using three indices, one for each dimension. In this case, you are accessing the value at position (0, 1, 2). Follow the below examples to get the third element of the second array of the first array.


# Create 3D input array
arr = np.array([[[0,3,5,], [7,9,11]], [[13, 15, 17], [19, 21, 23]]])

# Access values of 3-Dimensional arrays using index
arr2 = arr[0, 1, 2]
print("Value at position (0, 1, 2):",arr2)

# Output;
# Value at position (0, 1, 2): 11

5. Use Negative Array Indexing

You can use negative indexing to access value of an array from the end. Follow the below examples to get the last element from the 1-dimensional array, and 2-dimensional array.


# Create 2D input array
arr = np.array([[0,3,5,7,9],[11,13,15,17,19]])

# Use negative array index
arr2 = arr[0, -1]
print('Last element from 1st dimensional:',arr2)

# Output:
# Last element from 1st dimensional: 9

# Access the last element of the last row 
# Using negative indexing
arr2 = arr[1, -1]
print('Last element from 2nd dimensional:',arr2)

# Output:
# Last element from 2nd dimensional: 19

Frequently Asked Questions

What is NumPy array indexing?

NumPy array indexing is a way to access and manipulate the elements of a NumPy array. It allows you to select specific elements, slices, or subarrays based on their position or certain conditions.

How does basic indexing work in NumPy?

asic indexing in NumPy involves accessing individual elements of an array using integer indices. Indices in NumPy are 0-based, meaning the first element is at index 0.

How does integer array indexing work?

Integer array indexing allows you to access elements from an array using another array of integers as indices. For example, arr[[1, 3, 5]] will return the elements at indices 1, 3, and 5.

What is negative indexing in NumPy?

Negative indexing in NumPy allows you to access elements from the end of an array by using negative integers as indices. The index -1 corresponds to the last element, -2 to the second-to-last element, and so on.

Can I combine multiple indexing techniques?

You can combine different indexing techniques. For instance, you can use slicing along with integer or boolean indexing to create more complex selections

Can I use multiple indices for multidimensional arrays?

You can use multiple indices to access elements in multidimensional arrays in NumPy. The number of indices you use should match the number of dimensions in your array.

Conclusion

In this article, I have explained how to access values of the NumPy array in different ways by using array indexing with examples.

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply