In this article, we will show you how to sort an array in NumPy by the nth column both in ascending and descending order in python. NumPy is a Python library designed to work efficiently with arrays in Python. It is fast, simple to learn, and efficient in...
In this article, we will show you how to sort an array in NumPy by the nth column both in ascending and descending order in python.
NumPy is a Python library designed to work efficiently with arrays in Python. It is fast, simple to learn, and efficient in storage. It also improves the way data is handled for the process. In NumPy, we may generate an n-dimensional array. To use NumPy, we simply import it into our program, and then we can easily use NumPy's functionality in our code.
In this method, we generate a random NumPy array using a random module and sort the 1st column of the array in ascending order(default).
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword, to import the numpy module with an alias name(np).
Create a NumPy array of shape 6x6 with any random numbers from 0 to 19 using the random.randint() method(Returns a random number within the specified range).
Print the input array.
Use the argsort() function(perform an indirect sort along the specified axis using the algorithm provided by the kind keyword. It returns the indices which would sort an array), to sort the input array by the 1st column and print the resultant sorted array in ascending order of the 1st column.
The following program sorts the NumPy array by the 1st column in ascending order using argsort() function and returns it −
On executing, the above program will generate the following output −
The input random array is: [[ 8 16 7 5 13] [ 5 8 6 0 2] [11 7 3 3 6] [ 5 3 15 7 5] [15 3 9 14 3]] Sorting the input array by the 1st column: [[ 5 3 15 7 5] [15 3 9 14 3] [11 7 3 3 6] [ 5 8 6 0 2] [ 8 16 7 5 13]]
In this method, we generate a random NumPy array using a random module and sort the given nth column of the array in ascending order(default).
Following are the Algorithm/steps to be followed to perform the desired task −
Create a NumPy array of shape 4x4 with any random numbers from 0 to 99 using the random.randint() method and print the input array.
Enter n value and create a variable to store it.
Use the argsort() function, to sort the input array by the nth column and print the resultant sorted array in ascending order of the 1st column.
The following program sorts the NumPy array by the given nth column in ascending order using argsort() function and returns it −
On executing, the above program will generate the following output −
The input random array is: [[32 5 68 67] [ 7 7 12 63] [49 49 10 15] [96 5 93 29]] Sorting the input array by the 2 column: [[49 49 10 15] [ 7 7 12 63] [32 5 68 67] [96 5 93 29]]
In this method, we generate a random NumPy array using a random module and sort the given nth column of the array in descending order by slicing it in reverse order.
The following program sorts the NumPy array by the given nth column in descending order using argsort() function and returns it −
On executing, the above program will generate the following output −
The input random array is: [[47 6 62 53 50] [ 5 30 13 10 33] [43 93 57 91 91] [84 40 21 55 9] [76 89 85 3 4]] Sorting the input array by the 3 column: [[43 93 57 91 91] [84 40 21 55 9] [47 6 62 53 50] [ 5 30 13 10 33] [76 89 85 3 4]]
In this article, We learned how to sort an array in Numpy by the nth column and how to sort it in descending order using slicing.