In this tutorial, we are going to discuss how to sort the NumPy array by column or row in Python. Just click on the direct links available here and directly jump into the example codes on sorting 2D Numpy Array by Column or Row in Python.
How to Sort the NumPy Array by Column in Python?
In this section, you will be learning the concept of Sorting 2D Numpy Array by a column
Firstly, we have to import a numpy module ie.,
import numpy as np
After that, create a 2D Numpy array i.e.,
# Create a 2D Numpy array list of list arr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]]) print('2D Numpy Array') print(arr2D)
Output:
2D Numpy Array [[21 22 23 20] [21 17 13 14] [13 10 33 19]]
Suppose if we want to sort this 2D array by 2nd column like this,
[[21 7 23 14] [31 10 33 7] [11 12 13 22]]
To do that, first, we have to change the positioning of all rows in the 2D numpy array on the basis of sorted values of the 2nd column i.e. column at index 1.
Do Check:
Let’s see how to sort it,
Sorting 2D Numpy Array by column at index 1
In this, we will use arr2D[:,columnIndex].argsort()
which will give the array of indices that sort this column.
import numpy as np # Create a 2D Numpy array list of list arr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]]) print('2D Numpy Array') print(arr2D) columnIndex = 1 # Sort 2D numpy array by 2nd Column sortedArr = arr2D[arr2D[:,columnIndex].argsort()] print('Sorted 2D Numpy Array') print(sortedArr)
Output:
2D Numpy Array [[21 22 23 20] [21 17 13 14] [13 10 33 19]] Sorted 2D Numpy Array [[13 10 33 19] [21 17 13 14] [21 22 23 20]]
So in the above example, you have seen we changed the position of all rows in an array on sorted values of the 2nd column means column at index 1.
Sorting 2D Numpy Array by column at index 0
Let’s see how it will work when we give index 0.
import numpy as np # Create a 2D Numpy array list of list arr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]]) print('2D Numpy Array') print(arr2D) # Sort 2D numpy array by first column sortedArr = arr2D[arr2D[:,0].argsort()] print('Sorted 2D Numpy Array') print(sortedArr)
Output:
2D Numpy Array [[21 22 23 20] [21 17 13 14] [13 10 33 19]] Sorted 2D Numpy Array [[13 10 33 19] [21 22 23 20] [21 17 13 14]]
Sorting 2D Numpy Array by the Last Column
import numpy as np # Create a 2D Numpy array list of list arr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]]) print('2D Numpy Array') print(arr2D) # Sort 2D numpy array by last column sortedArr = arr2D[arr2D[:, -1].argsort()] print('Sorted 2D Numpy Array') print(sortedArr)
Output:
2D Numpy Array [[21 22 23 20] [21 17 13 14] [13 10 33 19]] Sorted 2D Numpy Array [[21 17 13 14] [13 10 33 19] [21 22 23 20]]
How to Sort the NumPy array by Row in Python?
By using similar logic, we can also sort a 2D Numpy array by a single row i.e. mix-up the columns of the 2D numpy array to get the furnished row sorted.
Look at the below examples and learn how it works easily,
Let’s assume, we have a 2D Numpy array i.e.
# Create a 2D Numpy array list of list arr2D = np.array([[11, 12, 13, 22], [21, 7, 23, 14], [31, 10, 33, 7]]) print('2D Numpy Array') print(arr2D)
Output:
2D Numpy Array [[11 12 13 22] [21 7 23 14] [31 10 33 7]]
Sorting 2D Numpy Array by row at index position 1
So we are going to use the above example to show how we sort an array by row.
import numpy as np # Create a 2D Numpy array list of list arr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]]) print('2D Numpy Array') print(arr2D) # Sort 2D numpy array by 2nd row sortedArr = arr2D [ :, arr2D[1].argsort()] print('Sorted 2D Numpy Array') print(sortedArr)
Output:
2D Numpy Array [[21 22 23 20] [21 17 13 14] [13 10 33 19]] Sorted 2D Numpy Array [[23 20 22 21] [13 14 17 21] [33 19 10 13]]
So you can see that it changed column value, as we selected row at given index position using [] operator and using argsort()
we got sorted indices after that we have changed the position of the column to sort our row.
Sorting 2D Numpy Array by the Last Row
import numpy as np # Create a 2D Numpy array list of list arr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]]) print('2D Numpy Array') print(arr2D) # Sort 2D numpy array by last row sortedArr = arr2D[:, arr2D[-1].argsort()] print('Sorted 2D Numpy Array') print(sortedArr)
Output:
2D Numpy Array [[21 22 23 20] [21 17 13 14] [13 10 33 19]] Sorted 2D Numpy Array [[22 21 20 23] [17 21 14 13] [10 13 19 33]]
Conclusion:
So in this article, I have shown you different ways to sorting 2D Numpy Array by column or row in Python.
Happy learning guys!