How to 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 here.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

Pandas: How to Sort by Date
Pandas: How to Sort Columns by Name
Pandas: How to Sort by Both Index and Column

Leave a Reply

Your email address will not be published.