Table of Contents
Sorting a NumPy array by column refers to arranging the elements of the array in ascending or descending order based on a specific column. This is achieved by using the “np.sort” function and specifying the axis as the desired column. For example, if we have a 3×3 array and want to sort it by the second column, we would use the code “np.sort(array, axis=1)” where “array” is the name of the array. This will result in the elements of the second column being arranged in ascending order while maintaining the corresponding elements in the other columns. Some examples of sorting by column in NumPy arrays include sorting a dataset by a specific attribute, organizing data in a table, or arranging values in a matrix.
Sort a NumPy Array by Column (With Examples)
You can use the following methods to sort the rows of a NumPy array by column values:
Method 1: Sort by Column Values Ascending
x_sorted_asc = x[x[:, 1].argsort()]
Method 2: Sort by Column Values Descending
x_sorted_desc = x[x[:, 1].argsort()[::-1]]The following examples show how to use each method in practice.
Example 1: Sort Numpy Array by Column Values Ascending
Suppose we have the following NumPy array:
import numpy as np #create array x = np.array([14, 12, 8, 10, 5, 7, 11, 9, 2]).reshape(3,3) #view array print(x) [[14 12 8] [10 5 7] [11 9 2]]
We can use the following code to sort the rows of the NumPy array in ascending order based on the values in the second column:
#define new matrix with rows sorted in ascending order by values in second column
x_sorted_asc = x[x[:, 1].argsort()]
#view sorted matrix
print(x_sorted_asc)
[[10 5 7]
[11 9 2]
[14 12 8]]Notice that the rows are now sorted in ascending order (smallest to largest) based on the values in the second column.
Example 2: Sort Numpy Array by Column Values Descending
Suppose we have the following NumPy array:
import numpy as np #create array x = np.array([14, 12, 8, 10, 5, 7, 11, 9, 2]).reshape(3,3) #view array print(x) [[14 12 8] [10 5 7] [11 9 2]]
We can use the following code to sort the rows of the NumPy array in descending order based on the values in the second column:
#define new matrix with rows sorted in descending order by values in second column
x_sorted_desc = x[x[:, 1].argsort()[::-1]]
#view sorted matrix
print(x_sorted_desc)
[[14 12 8]
[11 9 2]
[10 5 7]]Additional Resources
The following tutorials explain how to perform other common operations in Python:
Cite this article
stats writer (2024). How do I sort a NumPy array by column? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-sort-a-numpy-array-by-column-can-you-provide-some-examples/
stats writer. "How do I sort a NumPy array by column? Can you provide some examples?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-i-sort-a-numpy-array-by-column-can-you-provide-some-examples/.
stats writer. "How do I sort a NumPy array by column? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-sort-a-numpy-array-by-column-can-you-provide-some-examples/.
stats writer (2024) 'How do I sort a NumPy array by column? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-sort-a-numpy-array-by-column-can-you-provide-some-examples/.
[1] stats writer, "How do I sort a NumPy array by column? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How do I sort a NumPy array by column? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
