How to Swap Two Rows in a NumPy Array
Last Updated :
28 Apr, 2025
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]]
Similar Reads
How to swap columns of a given NumPy array? Swapping columns of a NumPy array means exchanging the positions of two specified columns across all rows. For example, if you have a 3x3 array with values like [[0, 1, 2], [3, 4, 5], [6, 7, 9]] and you swap column 0 with column 2, the array becomes [[2, 1, 0], [5, 4, 3], [9, 7, 6]]. Letâs explore d
4 min read
NumPy Array Sorting | How to sort NumPy Array Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.ar
4 min read
How to Set Axis for Rows and Columns in NumPy ? In this article, we are going to see how to set the axis for rows and columns in NumPy. Functions Usednp.array(object): to create a NumPy array, the object is the parameter that contains the arraynp.reshape(rows, columns): to reshape the array into the specified number of rows and columns. Here in t
3 min read
How to randomly select rows of an array in Python with NumPy ? In this article, we will see two different methods on how to randomly select rows of an array in Python with NumPy. Let's see different methods by which we can select random rows of an array: Method 1: We will be using the function shuffle(). The shuffle() function shuffles the rows of an array rand
2 min read
How to skip every Nth index of NumPy array ? NumPy arrays offer efficient numerical operations and data storage. When working with large arrays, sometimes it's necessary to skip specific indices for optimization or data processing purposes. This article will show how to skip every Nth index of the NumPy array. There are various ways to access
4 min read