Sorting
Pandas Basics
1 min read
This section is 1 min read, full guide is 30 min read
Published Sep 29 2025, updated Oct 24 2025
21
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
PandasPython
Sorting allows you to reorder rows based on column values.
Sample data for the examples:
import pandas as pd
df = pd.DataFrame({
"Name": ["Alice", "Bob", "Charlie", "David"],
"Age": [25, 30, 35, 40],
"Salary": [50000, 60000, 75000, 80000]
})
Copy to Clipboard
Sort by Single Column
# Sort by Age ascending (default)
df.sort_values("Age", inplace=True)
Copy to Clipboard
Toggle show comments
Sort Descending
df.sort_values("Salary", ascending=False, inplace=True)
Copy to Clipboard
Sort by Multiple Columns
# Sort by Age ascending, then Salary descending
df.sort_values(["Age", "Salary"], ascending=[True, False], inplace=True)
Copy to Clipboard
Toggle show comments
Resetting Index after Sorting
After filtering or sorting, the index may be non-sequential:
filtered_df = df.sort_values(["Age", "Salary"], ascending=[True, False],).reset_index(drop=True)
Copy to Clipboard
drop=True removes the old index.
Limit the sorted results
.head(n)shows the first n rows (default 5)..tail(n)shows the last n rows.
df.sort_values("Salary", ascending=False, inplace=True).head(2)
Copy to Clipboard
Sorts salary descending and returns just the top 2 rows.














