{"id":5209,"date":"2023-10-28T16:27:36","date_gmt":"2023-10-28T10:57:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=5209"},"modified":"2023-11-10T12:04:09","modified_gmt":"2023-11-10T06:34:09","slug":"sorting-2d-numpy-array-by-column-or-row-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/sorting-2d-numpy-array-by-column-or-row-in-python\/","title":{"rendered":"Sorting 2D Numpy Array by column or row in Python | How to sort the\u00a0NumPy array by column or row in Python?"},"content":{"rendered":"
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.<\/p>\n
In this section, you will be learning the concept of Sorting 2D Numpy Array by a column<\/p>\n
Firstly, we have to import a numpy module ie.,<\/p>\n
After that, create a 2D Numpy array i.e.,<\/p>\n Output:<\/strong><\/p>\n Suppose if we want to sort this 2D array by 2nd column like this,<\/p>\n 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.<\/p>\n Do Check:<\/span><\/p>\n Let\u2019s see how to sort it,<\/p>\n In this, we will use Output:<\/strong><\/p>\n 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.<\/p>\n Let’s see how it will work when we give index 0.<\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Look at the below examples and learn how it works easily,<\/p>\n Let’s assume, we have a 2D Numpy array i.e.<\/p>\n Output:<\/strong><\/p>\n So we are going to use the above example to show how we sort an array by row.<\/p>\n Output:<\/strong><\/p>\n So you can see that it changed column value, as we selected row at given index position using [] operator and using Output:<\/strong><\/p>\n So in this article, I have shown you different ways to sorting 2D Numpy Array by column or row in Python.<\/p>\n Happy learning guys!<\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\nimport numpy as np<\/code><\/p>\n
# Create a 2D Numpy array list of list\r\narr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]])\r\nprint('2D Numpy Array')\r\nprint(arr2D)<\/pre>\n
2D Numpy Array\r\n[[21 22 23 20]\r\n[21 17 13 14]\r\n[13 10 33 19]]\r\n<\/pre>\n
[[21 7 23 14]\r\n[31 10 33 7]\r\n[11 12 13 22]]<\/pre>\n
\n
<\/a>Sorting 2D Numpy Array by column at index 1<\/h3>\n
arr2D[:,columnIndex].argsort()<\/code>which will give the array of indices that sort this column.<\/p>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array list of list\r\narr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]])\r\nprint('2D Numpy Array')\r\nprint(arr2D)\r\ncolumnIndex = 1\r\n# Sort 2D numpy array by 2nd Column\r\nsortedArr = arr2D[arr2D[:,columnIndex].argsort()]\r\nprint('Sorted 2D Numpy Array')\r\nprint(sortedArr)<\/pre>\n
2D Numpy Array\r\n[[21 22 23 20]\r\n[21 17 13 14]\r\n[13 10 33 19]]\r\n\r\nSorted 2D Numpy Array\r\n[[13 10 33 19]\r\n[21 17 13 14]\r\n[21 22 23 20]]\r\n\r\n<\/pre>\n
<\/a>Sorting 2D Numpy Array by column at index 0<\/h3>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array list of list\r\narr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]])\r\nprint('2D Numpy Array')\r\nprint(arr2D)\r\n# Sort 2D numpy array by first column\r\nsortedArr = arr2D[arr2D[:,0].argsort()]\r\nprint('Sorted 2D Numpy Array')\r\nprint(sortedArr)\r\n<\/pre>\n
2D Numpy Array\r\n[[21 22 23 20]\r\n[21 17 13 14]\r\n[13 10 33 19]]\r\n\r\nSorted 2D Numpy Array\r\n[[13 10 33 19]\r\n[21 22 23 20]\r\n[21 17 13 14]]\r\n<\/pre>\n
<\/a>Sorting 2D Numpy Array by the Last Column<\/h3>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array list of list\r\narr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]])\r\nprint('2D Numpy Array')\r\nprint(arr2D)\r\n# Sort 2D numpy array by last column\r\nsortedArr = arr2D[arr2D[:, -1].argsort()]\r\nprint('Sorted 2D Numpy Array')\r\nprint(sortedArr)<\/pre>\n
2D Numpy Array\r\n[[21 22 23 20]\r\n[21 17 13 14]\r\n[13 10 33 19]]\r\n\r\nSorted 2D Numpy Array\r\n[[21 17 13 14]\r\n[13 10 33 19]\r\n[21 22 23 20]]\r\n<\/pre>\n
<\/a>How to Sort the NumPy array by Row in Python?<\/h2>\n
# Create a 2D Numpy array list of list\r\narr2D = np.array([[11, 12, 13, 22], [21, 7, 23, 14], [31, 10, 33, 7]])\r\nprint('2D Numpy Array')\r\nprint(arr2D)<\/pre>\n
2D Numpy Array\r\n[[11 12 13 22]\r\n[21 7 23 14]\r\n[31 10 33 7]]<\/pre>\n
<\/a>Sorting 2D Numpy Array by row at index position 1<\/h3>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array list of list\r\narr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]])\r\nprint('2D Numpy Array')\r\nprint(arr2D)\r\n# Sort 2D numpy array by 2nd row\r\nsortedArr = arr2D [ :, arr2D[1].argsort()]\r\nprint('Sorted 2D Numpy Array')\r\nprint(sortedArr)\r\n<\/pre>\n
2D Numpy Array\r\n[[21 22 23 20]\r\n[21 17 13 14]\r\n[13 10 33 19]]\r\n\r\nSorted 2D Numpy Array\r\n[[23 20 22 21]\r\n[13 14 17 21]\r\n[33 19 10 13]]\r\n\r\n<\/pre>\n
argsort()<\/code>we got sorted indices after that we have changed the position of the column to sort our row.<\/p>\n
<\/a>Sorting 2D Numpy Array by the Last Row<\/h3>\n
import numpy as np\r\n\r\n# Create a 2D Numpy array list of list\r\narr2D = np.array([[21, 22, 23, 20], [21, 17, 13, 14], [13, 10, 33, 19]])\r\nprint('2D Numpy Array')\r\nprint(arr2D)\r\n# Sort 2D numpy array by last row\r\nsortedArr = arr2D[:, arr2D[-1].argsort()]\r\nprint('Sorted 2D Numpy Array')\r\nprint(sortedArr)<\/pre>\n
2D Numpy Array\r\n[[21 22 23 20]\r\n[21 17 13 14]\r\n[13 10 33 19]]\r\n\r\nSorted 2D Numpy Array\r\n[[22 21 20 23]\r\n[17 21 14 13]\r\n[10 13 19 33]]\r\n<\/pre>\n
Conclusion:<\/h3>\n