You can sort the NumPy array by column with the following code. I highly recommend you the “Python Crash Course Book” to learn Python. In this article, you’ll see two examples with solutions.
Example 1: Sort NumPy array by column in Ascending order
# Import the NumPy library as np
import numpy as np
# Create a 1D NumPy array
arr = np.array([[5, 10, 15, 20, 5],
[13, 4, 25, 16, 7],
[10, 5, 3, 2, 5]])
# Sort array by 3rd column
output = arr[arr[:, 2].argsort()]
# Display the output
print(output)Output: Sorted array by 3rd Column
[[10 5 3 2 5] [ 5 10 15 20 5] [13 4 25 16 7]]
Example 2: Sort in Descending order
# Import the NumPy library as np
import numpy as np
# Create a 1D NumPy array
arr = np.array([[5, 10, 15, 20, 5],
[13, 4, 25, 16, 7],
[10, 5, 3, 2, 5]])
# Sort array by 3rd column
output = arr[arr[:, 2].argsort()[::-1]]
# Display the output
print(output)Output: Sorted array by 3rd Column
[[13 4 25 16 7] [ 5 10 15 20 5] [10 5 3 2 5]]


