Open In App

How to Swap Two Rows in a NumPy Array

Last Updated : 29 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

One common task you might encounter when working with NumPy arrays is the need to swap two rows. Swapping rows can be essential in data preprocessing, reshaping data, or reordering data to perform specific analyses in Python. In this article, we will explore different methods to swap two rows in a NumPy array. In this article, we will see how to swap two rows in a NumPy Array.

Swapping Two Rows in NumPy array

Below are some methods and ways by which we can swap two rows in a NumPy Array:

Swapping Rows using np.roll()

In this example, we are using numpy.roll() to swap two rows in a NumPy Array. As we can see in the given example, the 0th row is swapped with the 2nd row, the 1st with the 3rd row, and so on.

Python3




# Swapping two rows in a given numPy array
 
# Importing NumPy Module
import numpy as np
 
 
# Creating a NumPy array
num_arr = np.array([[1,3,1], [3,1,3], [2,9,2], [9,2,9]])
 
# Displaying the original array
print("Original array:")
print(num_arr)
 
# Using np.roll() method for swapping array along row
num_arr = np.roll(num_arr,2,axis=0)
 
print("\nArray after swapping the rows:")
print(num_arr)


Output

Original array:
[[1 3 1]
[3 1 3]
[2 9 2]
[9 2 9]]

Array after swapping the rows:
[[2 9 2]
[9 2 9]
[1 3 1]
[3 1 3]]

Swapping Rows of a NumPy Array using Advanced Indexing

In this example, we are using advanced indexing to swap two rows in a NumPy Array. Here, 0th and 3rd rows of NumPy array are swapped using advance indexing.

Python3




# Swapping two rows in a given numPy array
 
# Importing NumPy Module
import numpy as np
 
 
# Creating a NumPy array
num_arr = np.array([[1,3,1], [3,1,3], [2,9,2], [9,2,9]])
 
# Displaying the original array
print("Original array:")
print(num_arr)
 
# Swapping 0th and 3rd rows
num_arr[[0,3]] = num_arr[[3,0]]
 
print("\nArray after swapping the rows:")
print(num_arr)


Output:

Original array:
[[1 3 1]
[3 1 3]
[2 9 2]
[9 2 9]]
Array after swapping the rows:
[[9 2 9]
[3 1 3]
[2 9 2]
[1 3 1]]

Python Swapping Rows using NumPy Indexing

In this example, we are using NumPy indexing to swap two rows in a NumPy Array. Here, 0th, 1st and 3nd rows of NumPy array are swapped using numpy indexing.

Python3




# Swapping two rows in a given numPy array
 
# Importing NumPy Module
import numpy as np
 
 
# Creating a NumPy array
num_arr = np.array([[3, 2, 1], [6, 5, 4], [9, 8, 7]])
 
# Displaying the original array
print("Original array:")
print(num_arr)
 
# Swapping 0th and 1st and 2nd rows
num_arr = num_arr[[2 , 0, 1]]
 
print("\nArray after swapping the rows:")
print(num_arr)


Output:

Original array:
[[3 2 1]
[6 5 4]
[9 8 7]]
Array after swapping the rows:
[[9 8 7]
[3 2 1]
[6 5 4]]

Swapping the Rows in NumPy Array using Direct Assignment

In the given example, 1st and 2nd rows of NumPy array are swapped using direct assignment.

Python3




# Swapping two rows in a given numPy array
 
# Importing NumPy Module
import numpy as np
 
 
# Creating a NumPy array
num_arr = np.array([[3, 2, 1], [6, 6, 6], [8, 8, 8]])
 
# Displaying the original array
print("Original array:")
print(num_arr)
 
# Swapping 1st and 2nd rows
temp = num_arr[1].copy()
num_arr[1] = num_arr[2]
num_arr[2] = temp
 
print("\nArray after swapping the rows:")
print(num_arr)


Output

Original array:
[[3 2 1]
[6 6 6]
[8 8 8]]
Array after swapping the rows:
[[3 2 1]
[8 8 8]
[6 6 6]]

Swapping the Rows in Python NumPy Array using User Input

In the given example, we are taking user input and then swapping the rows.

Python3




# Swapping two rows in a given numPy array
 
# Importing NumPy Module
import numpy as np
 
 
# Creating a NumPy array
num_arr = np.array([[1, 1, 1], [6, 6, 6], [8, 8, 8], [0, 0, 0]])
 
# Displaying the original array
print("Original array:")
print(num_arr)
 
 
# Defining Swap function
 
def Swap(arr, firstIndex, secondIndex):
    arr[[firstIndex, secondIndex]] = arr[[secondIndex, firstIndex]]
 
# Passing parameter to Swap function
Swap(num_arr, 0, 3)
 
print("\nArray after swapping the rows:")
print(num_arr)


Output

Original array:
[[1 1 1]
[6 6 6]
[8 8 8]
[0 0 0]]
Array after swapping the rows:
[[0 0 0]
[6 6 6]
[8 8 8]
[1 1 1]]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads