Extract the first and second elements of the rows from an array

In this python numpy program, we will extract the first and second elements of the rows from an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Extract the first and second elements of the first and second rows from the array using indexing.
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])

print(x[0:2,0:2])
				
			

Output :

				
					[[ 6  5]
 [10  9]]
				
			

access first two columns of a 3-D array.

Leave a Comment