How to Sort by Multiple Columns Ascending and Descending in Pandas?

To sort by multiple columns ascending and descending in Pandas we can use syntax like:

df.sort_values(by=['name', 'salary'], ascending=[True, False])

Let's cover two examples to explain sorting on multiple columns in more detail.

Sort a DataFrame by two or more columns

To sort Pandas DataFrame by two and more columns we can use parameter by:

df.sort_values(by=['name', 'age'], ascending=True)

Example of sorting DataFrame by two columns:

import pandas as pd

df = pd.DataFrame({
	'name': ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob'],
	'age': [25, 30, 35, 25, 40],
	'salary': [50000, 70000, 60000, 55000, 80000]
})

df.sort_values(by=['name', 'age'], ascending=True)

The code above sort the DataFrame by 'name' and 'age' columns in ascending order:

name age salary
0 Alice 25 50000
3 Alice 25 55000
1 Bob 30 70000
4 Bob 40 80000
2 Charlie 35 60000

DataFrame before sorting is:

name age salary
0 Alice 25 50000
1 Bob 30 70000
2 Charlie 35 60000
3 Alice 25 55000
4 Bob 40 80000

We can specify the sort order for each column using the ascending parameter as boolean or a list:

  • ascending=True - the columns will be sorted in ascending order
  • ascending=False - sorted in descending order.

sort by multiple columns one ascending and descending

We can use Pandas method sort_values() to sort by multiple columns in different order: ascending and descending.

Parameter ascending can take a list of values: ascending=[True, False].

Full example of sort both ascending and descending in Pandas:

import pandas as pd

df = pd.DataFrame({
	'name': ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob'],
	'age': [25, 30, 35, 25, 40],
	'salary': [50000, 70000, 60000, 55000, 80000]
})

df.sort_values(by=['name', 'salary'], ascending=[True, False])

The result of the sorted DataFrame is:

name age salary
3 Alice 25 55000
0 Alice 25 50000
4 Bob 40 80000
1 Bob 30 70000
2 Charlie 35 60000

Resources