Skip to main content

How to Sort a pandas DataFrame by a Column

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Sorting a table by one column is how you find its top or bottom rows: the most expensive houses, or the predictions a model got most wrong. DataFrame.sort_values does it in one call, and two of its options cover cases that trip people up: sorting by size regardless of sign, and renumbering the rows afterwards. This guide sorts a small table of prediction errors, with real output from pandas 3.1.1.

Syntax​

Syntax
<table>.sort_values("<column>")
<table>.sort_values("<column>", ascending=False)
<table>.sort_values("<column>", key=<function>, ignore_index=True)
PartWhat it means
"<column>"The column to sort by. A list of names sorts by several, in order
ascending=FalseLargest first. The default, True, is smallest first
key=<function>"Apply the key function to the values before sorting." pandas passes the whole column, so the function "should expect a Series and return a Series with the same shape"
ignore_index=True"The resulting axis will be labeled 0, 1, …, n - 1": the rows are renumbered after sorting

sort_values returns a new, sorted DataFrame. The original stays in its old order.

Step 1: Sort a Table Three Ways​

The example is four houses and how far a model's price prediction was from the real price, in hundreds of thousands: negative means too low, positive means too high. Create sort_values.py:

The sort_values.py script in a macOS-style code window: an errors DataFrame with houses A to D and errors -1.0, 0.5, 0.1 and -3.2, then three prints: sorted by error, sorted by error with ascending False, and sorted by error with key abs, ascending False and ignore_index True.

sort_values.py
import pandas as pd

errors = pd.DataFrame({"house": ["A", "B", "C", "D"], "error": [-1.0, 0.5, 0.1, -3.2]})

print(errors.sort_values("error"))
print(errors.sort_values("error", ascending=False))
print(errors.sort_values("error", key=abs, ascending=False, ignore_index=True))
LineWhat it does
sort_values("error")Smallest error first, so the biggest underestimate comes first
ascending=FalseLargest error first, so the biggest overestimate comes first
key=abs, ascending=False, ignore_index=TrueSorts by the error's size, ignoring its sign, biggest miss first, and numbers the rows 0 to 3

abs is Python's built-in absolute value. Given a pandas column, it returns the column of absolute values, which is exactly what key needs.

Step 2: Run It​

Terminal
uv run python sort_values.py
Output
house error
3 D -3.2
0 A -1.0
2 C 0.1
1 B 0.5
house error
1 B 0.5
2 C 0.1
0 A -1.0
3 D -3.2
house error
0 D -3.2
1 A -1.0
2 B 0.5
3 C 0.1

A terminal running uv run python sort_values.py: three tables of houses and errors, sorted smallest first with D at -3.2 on top, largest first with B at 0.5 on top, and by absolute size with the rows renumbered 0 to 3: D -3.2, A -1.0, B 0.5, C 0.1.

  • Smallest first puts house D, -3.2, on top. Each row keeps its original label on the left: D is still row 3.
  • Largest first puts B, +0.5, on top. By value, D is now last.
  • By size puts D first, because 3.2 is the biggest miss, then A, then B, then C. The sign no longer matters, and ignore_index=True relabelled the rows 0 to 3 in their new order.

Common Mistakes​

MistakeWhat happensDo this instead
Calling errors.sort_values("error") without keeping the resulterrors is unchanged, because sort_values returns a new tableAssign it: errors = errors.sort_values("error")
Sorting a signed column largest first to find the worst errorsBig negative errors end up lastSort with key=abs
Misspelling the columnKeyError: 'eror'Copy the name from errors.columns
A key function written for one number, such as lambda x: x if x > 0 else -xIt receives the whole column: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.item(), a.any() or a.all().Use functions that work on a whole column, such as abs or lambda s: s.abs()

Troubleshooting​

SymptomLikely causeFix
The row numbers look scrambled after sortingRows keep their original labelsAdd ignore_index=True
Empty values appear at the bottomna_position defaults to "last"Pass na_position="first" to put them on top

FAQ​

How do I sort by two columns?​

Pass a list, and a list of directions if they differ: sort_values(["house", "error"], ascending=[True, False]).

Can I sort the table in place?​

Yes, with inplace=True, which changes the table and returns None. Assigning the result, as above, is clearer and works the same.

How do I get only the top five rows?​

Sort, then take the first five: errors.sort_values("error", key=abs, ascending=False).head(5).

Conclusion​

sort_values("error") sorted four prediction errors smallest first, ascending=False reversed it, and key=abs with ignore_index=True sorted them by size whatever their sign, biggest miss first, with the rows renumbered 0 to 3. Keep the result, because the original table doesn't change.

Additional Resources​