In this python numpy program, we will access two columns of a NumPy array.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Use for loop to iterate over the first two rows of the array.
- Access the first two columns of the array using indexing.
- Print the output.
import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])
for i in range(0,2):
print(f"{i+1} column: ",x[:,i])
Output :
1 column: [ 6 10 7]
2 column: [5 9 8]