Table of Contents
Multiple columns can be sorted in pandas by using the “sort_values()” function. This function allows for the sorting of one or more columns in ascending or descending order. The columns can be specified by name or by index position. Additionally, the “ascending” parameter can be set to True or False to determine the sorting direction. This feature is particularly useful when dealing with large datasets and allows for the organization and analysis of data in a more efficient manner.
Sort by Multiple Columns in Pandas (With Examples)
You can use the following basic syntax to sort a pandas DataFrame by multiple columns:
df = df.sort_values(['column1', 'column2'], ascending=(False, True))
The following example shows how to use this syntax in practice.
Example: Sort by Multiple Columns in Pandas
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [14, 20, 9, 20, 25, 29, 20, 25], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame df points assists rebounds 0 14 5 11 1 20 7 8 2 9 7 10 3 20 9 6 4 25 12 6 5 29 9 5 6 20 9 9 7 25 4 12
We can use the following syntax to sort the rows of the DataFrame by points ascending, then by assists descending:
#sort by points ascending, then assists ascending
df = df.sort_values(['points', 'assists'])
#view updated DataFrame
df
points assists rebounds
2 9 7 10
0 14 5 11
1 20 7 8
3 20 9 6
6 20 9 9
7 25 4 12
4 25 12 6
5 29 9 5
Notice that the rows are sorted by points ascending (smallest to largest), then by assists ascending.
We can also use the ascending argument to specify whether to sort each column in an ascending or descending manner:
#sort by points descending, then assists ascending
df = df.sort_values(['points', 'assists'], ascending = (False, True)))
#view updated DataFrame
df
points assists rebounds
5 29 9 5
7 25 4 12
4 25 12 6
1 20 7 8
3 20 9 6
6 20 9 9
0 14 5 11
2 9 7 10
Notice that the rows are sorted by points descending (largest to smallest), then by assists ascending.
In these examples we sorted the DataFrame by two columns, but we can use this exact syntax to sort by any number of columns that we’d like.
Note: You can find the complete documentation for the pandas sort_values() function .
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
Cite this article
stats writer (2024). How can multiple columns be sorted in pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-multiple-columns-be-sorted-in-pandas/
stats writer. "How can multiple columns be sorted in pandas?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-multiple-columns-be-sorted-in-pandas/.
stats writer. "How can multiple columns be sorted in pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-multiple-columns-be-sorted-in-pandas/.
stats writer (2024) 'How can multiple columns be sorted in pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-multiple-columns-be-sorted-in-pandas/.
[1] stats writer, "How can multiple columns be sorted in pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can multiple columns be sorted in pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
