Access rows of Mutidimesional NumPy array

In this post, we are going to understand how to Access rows of Mutidimesional NumPy array 2D or 3D with examples. To use the NumPy library and its function it must be installed on the local machine.

1. Access row of Mutidimesional NumPy array 2D


In this example Python example, we are accessing rows of 2D NumPy array matrix of shape 4X4. We have accessed the row First and third rows of the given array.

  • Imported the NumPy library using “import NumPy as np”
  • Created a NumPy array|matrix of shapes 4*4
  • Accessed the rows 1,3 of the given NumPy array
import numpy as np
npArr = np.array([[3,6,9,12],[15,18,21,24],[7,14,21,28],[35,42,49,56]])

print('Access rows of 2D array: \n',npArr[[1,3]])

Output

Access rows of 2D array: 
 [[15 18 21 24]
 [35 42 49 56]]

2. Access rows of Mutidimesional NumPy array 2D


In this Python example, we are accessing rows of 2D NumPy array of shape 5*5. We have accessed the row First and Second, the third row of the given array.

  • Imported the NumPy library using “import numpy as np”
  • Created an array of shapes 5*5
  • Accessed the row 1,2,3 of the given numpy array
import numpy as np
 
npArr = np.array([[3,6,9,12,30],[15,18,21,24,27],[7,14,21,28,32],
                  [35,42,49,56,63],[4,12,16,20,24]])
print('The first,Second, Third row of 2D array: \n',npArr[[1,2,3]])

Output

The first,Second, Third row of 2D array: 
 [[15 18 21 24 27]
 [ 7 14 21 28 32]
 [35 42 49 56 63]]

3. Access last rows of 2D NumPy array


In this Python example, we are accessing rows of 2D NumPy array of shape 5*5. We have accessed the row last of given array.

  • Imported the NumPy library using “import numpy as np”
  • Created an array of shapes 5*5.
  • Accessed the last row of the given NumPy array.
import numpy as np
 
npArr = np.array([[3,6,9,12,30],[15,18,21,24,27],[7,14,21,28,32],
                  [35,42,49,56,63],[4,12,16,20,24]])
print('The first,Second, Third row of 2D array: \n',npArr[[3]])

Output

The last row of 2D array: 
 [[35 42 49 56 63]]

4. Access Middle rows of 3D NumPy array


In this Python example, we will understand how to Access rows of the Multidimensional NumPy array. First, We have created a 3D NumPy array and accessed the middle row of given rows. Let us understand with the below example

  • Imported the NumPy library using “import numpy as np”
  • Created a 3D NumPy array
  • Accessed the middle row of the given NumPy array
import numpy as np
npArr = np.array([[[3,6,9],[15,18,21],[7,14,21]],
                 [[10,20,30],[12,24,36],[20,40,60]]])
 

print('select row from 3D array: \n',npArr[:,[1]])

Output

select row from 3D array: 
 [[[15 18 21]]

 [[12 24 36]]]

5. Access Last Row of 3D NumPy array


In this Python program example, we are accessing the last row from each 3D NumPy array by using npArr[:,[2]] code.

  • imported the NumPy library using “import numpy as np”
  • Created a 3D NumPy array
  • Accessed the middle row of the given NumPy array
import numpy as np
npArr = np.array([[[3,6,9],[15,18,21],[7,14,21]],
                 [[10,20,30],[12,24,36],[20,40,60]]])
 

print('select row from 2D array: \n',npArr[:,[2]])

Output

select row from 2D array: 
 [[[ 7 14 21]]

 [[20 40 60]]]