Data Science · Level 2 Ordering

Sorting a pandas DataFrame

Build a leaderboard (highest score first, ties by name) and renumber its rows with reset_index().

  • sort_values(by, ascending)
  • Sorting by several columns
  • reset_index(drop=True)

sort_values returns a new sorted DataFrame; pass lists to sort by several columns. Trap: rows keep their old index labels, so the index looks shuffled until you call reset_index(drop=True).

board = df.sort_values(["score", "name"], ascending=[False, True])
board = board.reset_index(drop=True)   # index 0, 1, 2...

Write leaderboard(df) that sorts players by score, highest first, then by name from A to Z for ties, with a fresh index, for example scores [5, 9, 9] for Cy, Bo and Al give the names ['Al', 'Bo', 'Cy'].

solution.py
Test Suites Pending
Output