0PricingLogin
Pandas & NumPy Academy · Lesson

Sorting by Column Values

Sort a DataFrame by one or more columns with sort_values(), control ascending/descending order, and handle NaN placement.

Why Sorting Matters

Sorting a DataFrame is important for presentation (top-N reports, leaderboards), correctness (time series operations require chronological order), and performance (binary search on a sorted index is O(log n) vs O(n)). Pandas' sort_values() method is the primary tool for sorting by column content, and it returns a new DataFrame without modifying the original.

import pandas as pd

df = pd.DataFrame({
    'name': ['Dave', 'Alice', 'Carol', 'Bob'],
    'score': [75, 92, 88, 65]
})

# Sort by score descending (highest first)
ranked = df.sort_values('score', ascending=False)
print(ranked)
#     name  score
# 1  Alice     92
# 2  Carol     88
# 0   Dave     75
# 3    Bob     65

sort_values() — Basic Usage

DataFrame.sort_values(by) sorts rows by the values in the specified column. The by argument accepts a single column name (string) or a list of column names for multi-key sorting. By default, sorting is ascending (smallest first). Pass ascending=False for descending order.

import pandas as pd

df = pd.DataFrame({
    'product': ['B', 'A', 'C', 'A', 'B'],
    'price': [20, 10, 30, 15, 25]
})

# Ascending by price (default)
print(df.sort_values('price'))
#   product  price
# 1       A     10
# 3       A     15
# 0       B     20
# 4       B     25
# 2       C     30

All lessons in this course

  1. Sorting by Column Values
  2. Sorting by Index
  3. Ranking Values
  4. Setting and Resetting the Index
← Back to Pandas & NumPy Academy