Python Numpy - Get Specific Row of Elements
Get a Row from Numpy Array
To get a specific row of elements, access the numpy array with all the specific index values for other dimensions and :
for the row of elements you want to retrieve. This is a special case of array slicing in Python.
For example, consider that we have a 3D numpy array with shape (m, n, p). If we would like to get the row of elements at the ith index along axis 0 and the kth index along axis 2, we can use the following syntax to obtain this desired row of elements:
row = ndarray[i, :, k]
Examples
1. Access a specific row of elements in a 3D numpy array
In this example, we will initialize a 3D array and access a specific row of elements present at index=0 along axis 0, and index=1 along axis 2.
Python Program
import numpy as np
#initialize an array
arr = np.array([[[11, 11, 9, 9],
[11, 0, 2, 0]],
[[10, 14, 9, 14],
[0, 1, 11, 11]]])
# print shape of array
print('Array Shape: ',arr.shape)
# get the desired row
row = arr[0, :, 1]
print('Desired Row of Elements: ', row)
Explanation:
- The array
arr
is initialized as a 3D array with shape (2, 2, 4). - The
arr[0, :, 1]
command accesses the row at index 0 along axis 0, and all the elements along axis 1, at index 1 along axis 2. - We print the shape of the array and the row retrieved.
Output
Array Shape: (2, 2, 4)
Desired Row of Elements: [11 0]
2. Access a specific row or column in a 2D numpy array
In this example, we will initialize a 2D array and access both a specific row and a column using array slicing.
Python Program
import numpy as np
#initialize an array
arr = np.array([[11, 11, 9, 9],
[11, 0, 2, 0]])
print('Array\n',arr)
# get row at index=1 along axis=0
row = arr[1, :]
print('arr[1, :] : ', row)
# get column at index=2 along axis=1
col = arr[:, 2]
print('arr[:, 2] : ', col)
Explanation:
- The array
arr
is initialized as a 2D array with two rows and four columns. - We access the row at index 1 along axis 0 with
arr[1, :]
and the column at index 2 along axis 1 witharr[:, 2]
. - We then print the results for both row and column.
Output
Array
[[11 11 9 9]
[11 0 2 0]]
arr[1, :] : [11 0 2 0]
arr[:, 2] : [ 9 2]
3. Access a specific element in a multi-dimensional array
Here, we demonstrate how to access a specific element within a multi-dimensional NumPy array by specifying its indices across all axes.
Python Program
import numpy as np
#initialize an array
arr = np.array([[[11, 11, 9, 9],
[11, 0, 2, 0]],
[[10, 14, 9, 14],
[0, 1, 11, 11]]])
# access an element at index 1 along axis 0, index 0 along axis 1, and index 2 along axis 2
element = arr[1, 0, 2]
print('Accessed Element: ', element)
Explanation:
- The array
arr
is initialized as a 3D array. - We use
arr[1, 0, 2]
to access the element at index 1 along axis 0, index 0 along axis 1, and index 2 along axis 2. - The accessed element is then printed.
Output
Accessed Element: 9
Summary
In this NumPy Tutorial, we demonstrated how to access specific rows from both 2D and 3D NumPy arrays using array slicing techniques. Additionally, we showed how to access columns and individual elements, helping you navigate and manipulate arrays effectively.