> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Sort by Multiple Columns Ascending and Descending in Pandas?
- URL: https://datascientyst.com/how-to-sort-by-multiple-columns-ascending-and-descending-in-pandas/
- Published: 2023-04-02T20:13:31.000Z
- Updated: 2023-04-02T20:21:19.000Z
- Author: John D K
- Tags: Other

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

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

```

![](https://datascientyst.com/content/images/2023/04/sort-by-multiple-columns-ascending-and-descending-in-pandas.webp)

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:

```python
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:

```python
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

- [pandas.DataFrame.sort\_values](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort%5Fvalues.html?ref=datascientyst.com)
- [pandas.DataFrame.sort\_index](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort%5Findex.html?ref=datascientyst.com)