In this python numpy program, we will swap rows of a given array using NumPy.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Swap rows of a given array using Indexing and logic.
- Print the output.
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
print("Original array: ",x)
print("After swapping")
new = print(x[::-1, :])
Output :
Original array: [[ 6 5]
[10 9]
[ 8 7]]
After swapping
[[ 8 7]
[10 9]
[ 6 5]]