0
0
PandasHow-ToBeginner · 3 min read

How to Sort by Multiple Columns in pandas DataFrame

Use the DataFrame.sort_values() method with a list of column names in the by parameter to sort by multiple columns. You can control the sort order for each column by passing a list of booleans to the ascending parameter.
📐

Syntax

The basic syntax to sort a pandas DataFrame by multiple columns is:

  • df.sort_values(by=[col1, col2, ...], ascending=[bool1, bool2, ...])

Here, by is a list of column names to sort by in order of priority. ascending is a list of booleans indicating whether to sort each column in ascending (True) or descending (False) order.

python
df.sort_values(by=['column1', 'column2'], ascending=[True, False])
💻

Example

This example shows how to sort a DataFrame first by 'Age' ascending, then by 'Score' descending.

python
import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
    'Age': [25, 30, 25, 30, 22],
    'Score': [85, 90, 95, 80, 88]
}
df = pd.DataFrame(data)

sorted_df = df.sort_values(by=['Age', 'Score'], ascending=[True, False])
print(sorted_df)
Output
Name Age Score 4 Eva 22 88 2 Charlie 25 95 0 Alice 25 85 1 Bob 30 90 3 David 30 80
⚠️

Common Pitfalls

Common mistakes include:

  • Passing a single boolean to ascending when sorting by multiple columns, which applies the same order to all columns.
  • Not passing a list to by, which causes errors.
  • Forgetting that sorting is stable, so the order of columns matters.

Example of wrong and right usage:

python
import pandas as pd

data = {'A': [2, 1, 2], 'B': [1, 2, 3]}
df = pd.DataFrame(data)

# Wrong: ascending is a single bool but sorting by two columns
try:
    df.sort_values(by=['A', 'B'], ascending=True)
except Exception as e:
    print(f'Error: {e}')

# Right: ascending is a list matching columns
sorted_df = df.sort_values(by=['A', 'B'], ascending=[True, False])
print(sorted_df)
Output
Error: If a list of columns is passed, ascending must be a list or tuple of booleans of the same length. A B 1 1 2 2 2 3 0 2 1
📊

Quick Reference

ParameterDescriptionExample
byList of columns to sort by['Age', 'Score']
ascendingList of booleans for sort order per column[True, False]
inplaceSort in place without creating new DataFrameTrue or False
na_positionPosition of NaNs: 'first' or 'last''last'

Key Takeaways

Use df.sort_values(by=[...], ascending=[...]) to sort by multiple columns in pandas.
The order of columns in 'by' defines sorting priority.
Pass a list of booleans to 'ascending' matching the number of columns.
Sorting is stable; earlier columns have higher priority.
Common errors come from mismatched 'by' and 'ascending' parameters.