0
0
PandasHow-ToBeginner · 3 min read

How to Sort DataFrame in pandas: Simple Guide

To sort a DataFrame in pandas, use df.sort_values() to sort by column values or df.sort_index() to sort by the index. You can specify ascending or descending order and sort by multiple columns easily.
📐

Syntax

The main method to sort a DataFrame by column values is df.sort_values(by=column_name, ascending=True). Use by to specify the column(s) to sort by. Set ascending=False to sort in descending order. To sort by index, use df.sort_index(ascending=True).

python
df.sort_values(by='column_name', ascending=True)
df.sort_index(ascending=True)
💻

Example

This example shows how to sort a DataFrame by one column in ascending order and by multiple columns with different orders.

python
import pandas as pd

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

# Sort by Age ascending
sorted_df = df.sort_values(by='Age')

# Sort by Age ascending, then Score descending
sorted_multi = df.sort_values(by=['Age', 'Score'], ascending=[True, False])

print('Sorted by Age:')
print(sorted_df)
print('\nSorted by Age and Score:')
print(sorted_multi)
Output
Sorted by Age: Name Age Score 0 Alice 25 85 2 Charlie 25 85 1 Bob 30 90 3 David 35 80 Sorted by Age and Score: Name Age Score 0 Alice 25 85 2 Charlie 25 85 1 Bob 30 90 3 David 35 80
⚠️

Common Pitfalls

  • Forgetting to assign the sorted DataFrame back to a variable or use inplace=True means the original DataFrame stays unsorted.
  • Using ascending as a single boolean when sorting by multiple columns causes unexpected results; use a list of booleans instead.
  • Sorting by a column name that does not exist will raise an error.
python
import pandas as pd

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

# Wrong: does not change df
sorted_wrong = df.sort_values(by='A')
print(df)  # Original order

# Right: assign or use inplace
sorted_right = df.sort_values(by='A')
print(sorted_right)

# Or inplace
# df.sort_values(by='A', inplace=True)
# print(df)
Output
A 0 3 1 1 2 2 A 1 1 2 2 0 3
📊

Quick Reference

MethodPurposeKey Parameters
sort_values(by, ascending=True, inplace=False)Sort by column valuesby: column(s) name(s), ascending: bool or list, inplace: bool
sort_index(ascending=True, inplace=False)Sort by indexascending: bool, inplace: bool

Key Takeaways

Use df.sort_values() to sort by one or more columns with control over ascending or descending order.
Remember to assign the result or use inplace=True to keep the sorted DataFrame.
Use df.sort_index() to sort rows by their index labels.
When sorting by multiple columns, pass a list of booleans to ascending to control each column's order.
Sorting by a non-existent column will cause an error, so check column names carefully.