Write a Python code to sort an array in NumPy by the nth column?

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.

Method 1. Sorting a Numpy Array by the 1st column

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).

Algorithm (Steps)

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.

Example

The following program sorts the NumPy array by the 1st column in ascending order using argsort() function and returns it −

 
# importing NumPy module with an alias name import numpy as np # creating a NumPy array of any random numbers from 0 to 19 of shape 6*6 inputArray = np.random.randint(0,20,(5,5)) # printing the input array print("The input random array is:") print(inputArray) # using argsort() function, to sort the input array by the 1st column print("Sorting the input array by the 1st column:") # Here in [:,1], ':' specifies all the rows and 1 specifies we need to sort by 1st column print(inputArray[inputArray[:,1].argsort()])

Output

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]]

Method 2. Sorting a Numpy Array by the nth column

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).

Algorithm (Steps)

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.

Example

The following program sorts the NumPy array by the given nth column in ascending order using argsort() function and returns it −

 
# importing NumPy module with an alias name import numpy as np # creating a NumPy array of any random numbers from 0 to 100 of shape 4x4 inputArray = np.random.randint(0,100,(4,4)) # printing the input array print("The input random array is:") print(inputArray) # entering the value of n n = 2 # using argsort() function, to sort the input array by the nth column # here we are sorting the nth column of the array print("Sorting the input array by the",n,"column:") # Here in [:,n], ':' specifies all the rows and n specifies we need to sort by nth column print(inputArray[inputArray[:,n].argsort()])

Output

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]]

Method 3. Sorting a Numpy Array by the nth column in reverse order

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.

Example

The following program sorts the NumPy array by the given nth column in descending order using argsort() function and returns it −

 
# importing numpy module with an alias name import numpy as np # creating a numpy array of any random numbers from 0 to 100 of shape 5*5 inputArray = np.random.randint(0,100,(5,5)) # printing the input array print("The input random array is:") print(inputArray) # entering the value of n n = 3 print("Sorting the input array by the",n,"column:") # Here in [:,n], ':' specifies all the rows and n specifies we need to sort by nth column # [::-1] slices the result in reverse order(descending order) print(inputArray[inputArray[:,n].argsort()[::-1]])

Output

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]]

Conclusion

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.

评论0

首页 导航 会员 客服