HomeCourseModule 09 › Sorting, counting, and ranking

Sorting, counting, and ranking

Module 09 · Pandas Crash Course7 min readBeginner

What you'll learn

  • Sort by one or more columns
  • Count occurrences of values
  • Rank a column

Sorting

df.sort_values("amount", ascending=False).head(10)
df.sort_values(["region", "amount"], ascending=[True, False])
df.sort_index()           # by row index

Counting

df["region"].value_counts()           # counts per region
df["region"].value_counts(normalize=True)   # as percentages
df["region"].nunique()                # distinct count

Ranking

df["rank_by_amount"] = df["amount"].rank(ascending=False)
df["pct_rank"] = df["amount"].rank(pct=True)

Top / bottom N

df.nlargest(10, "amount")
df.nsmallest(5, "amount")

Key takeaways

  • sort_values(["a","b"], ascending=[True,False]) handles multi-key sort.
  • value_counts() is the one-liner for a quick frequency table.
  • nlargest() / nsmallest() are faster and clearer than sort+head.

Bestseller list

From an orders DataFrame, print the top 5 products by total revenue.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.