In this tutorial, we will see how to access elements from a numpy array with the help of indexing  to obtain the values in the arrays or assigning new values to the elements.

Indexing in 1-D numpy arrays

Python uses square brackets [] to index the elements of an array. When we are using 1-D arrays, the index of the first element is 0 and it increases by 1 for each element  moving rightwards.

In the following example, a numpy array A has been defined and then it prints the elements at index 0,1 and 8.

import numpy as np
A=np.arange(1,10)
print("Array is :")
print(A)
print("first element of array is:")
print(A[0])
print("second element of array is:")
print(A[1])
print("last element of array is:")
print(A[8])

Output:

Array is :
[1 2 3 4 5 6 7 8 9]
first element of array is:
1
second element of array is:
2
last element of array is:
9

 

In python array elements can be accessed with the help of negative indices too. In this notation, the last element of an array has index -1 and the index decreases on moving from right to left in the array. 

In the example given below, last element of array A has been accessed by index -1 and second last element has been accessed by index -2.

import numpy as np
A=np.arange(1,10)
print("Array is :")
print(A)
print("last element of array is:")
print(A[-1])
print("second last element of array is:")
print(A[-2])
print("last element of array is:")
print(A[-9])

Output:

Array is :
[1 2 3 4 5 6 7 8 9]
last element of array is:
9
second last element of array is:
8
last element of array is:
1

How to select multiple elements from a numpy array?

If we have to select multiple elements at once, we can pass an array of indices in the square bracket. We can access consecutive elements as well as elements from randomly any position. All that we have to do is to pass an array of indices in square bracket. We can use negative indices too.

import numpy as np
A=np.arange(1,10)
print("Array is :")
print(A)
print("first four elements of array are:")
print(A[[0,1,2,3]])
print("last three elements of array are:")
print(A[[-3,-2,-1]])
print("4th, 6th, 3rd, 7th elements of array are:")
print(A[[3,5,2,6]])

Output:

Array is :
[1 2 3 4 5 6 7 8 9]
first four elements of array are:
[1 2 3 4]
last three elements of array are:
[7 8 9]
4th, 6th, 3rd, 7th elements of array are:
[4 6 3 7]

Indexing in 2-D Arrays

2-D arrays are represented as rectangular arrays which consist of rows and columns. Rows and columns are identified by two axes where rows are represented by axis 0 and columns are represented by axis 1. While indexing 2-D arrays we will need to specify the position of the element by row number and column number and thus indexing in 2-D arrays is represented using a pair of values. If we want to access the values or select elements from the array, we will use a square bracket with row index in first position and column index in second position. We can also use negative index for 2-D arrays.

In the example given below, the code prints elements at  the specified indexes.

import numpy as np
A=np.arange(1,10).reshape(3,3)
print("Array is :")
print(A)
print("Array element at first row first column is:")
print(A[0,0])
print("Array element at last row last column is:")
print(A[-1,-1])
print("Array element at position (1,1) is:")
print(A[1,1])

Output:

Array is :
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Array element at first row first column is:
1
Array element at last row last column is:
9
Array element at position (1,1) is:
5

How to select an entire row of a numpy array?

If we want to access an entire row then we can simply leave the column index empty or put a “:” at column index  while specifying the indices and only specify the row number. We can specify negative index too.

In the example given below, the code prints the first and last row of array A.

import numpy as np
A=np.arange(1,10).reshape(3,3)
print("Array is :")
print(A)
print("first row of the array is:")
print(A[0,:])
print("last row of the array is:")
print(A[-1,:])

Output:

Array is :
[[1 2 3]
 [4 5 6]
 [7 8 9]]
first row of the array is:
[1 2 3]
last row of the array is:
[7 8 9]

How to select an entire column of a numpy array?

If we want to access an entire column then we can simply use “:” in place of row index and specify the column index.

In the example given below, the code prints first and last columns of a 2-D array A.

import numpy as np
A=np.arange(1,10).reshape(3,3)
print("Array is :")
print(A)
print("first column of the array is:")
print(A[: ,0])
print("last column of the array is:")
print(A[ :,-1])

Output:

Array is :
[[1 2 3]
 [4 5 6]
 [7 8 9]]
first column of the array is:
[1 4 7]
last column of the array is:
[3 6 9]

Conclusion:

In this post we have seen how to access elements 0f 1-D and 2-D numpy arrays using different indexing methods. We have also seen how to access entire rows and columns of numpy arrays using indexing.

References:

Happy Learning!