Sort a Pandas DataFrame by Row Index

Quick Overview

Implement a pandas function that reorders a DataFrame's rows by their index labels in ascending order. It tests how sort_index works, how to keep the original labels without a reset, and how to return a new DataFrame without mutating the input.

Sort a Pandas DataFrame by Row Index

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def sort_by_index(df: pd.DataFrame) -> pd.DataFrame` that returns the rows reordered by row index labels ascending using `df.sort_index()`. The index may be an unsorted integer index. The function must also meet two requirements: - Preserve the original index values: no `reset_index()` or relabeling. - Do not mutate the input `df`: return a new DataFrame. Example: ```python df = pd.DataFrame({"a": [10, 20, 30]}, index=[2, 0, 1]) sort_by_index(df) # a # 0 20 # 1 30 # 2 10 ``` ```hint Mutation Check what `sort_index()` returns by default and which of its arguments would modify the caller's DataFrame. Only one of those behaviors meets the second requirement. ``` ### What a Strong Answer Covers - Orders the rows by their index labels with `sort_index()` in the default ascending direction. - Returns the new DataFrame without `inplace=True`, and can show that the input keeps its original order. - Keeps the original labels in the result, with no `reset_index()` and no `ignore_index=True`. - Handles edge cases: an empty frame, an index that is already sorted, duplicate labels and missing labels. ### Follow-up Questions - How would you sort in descending order, or sort the columns by their labels? - If the index has duplicate labels, what order do the tied rows come out in, and how would you guarantee they keep their original relative order? - How does `sort_index()` behave on a MultiIndex, and how would you sort by only one level? - Why can `df.reindex(sorted(df.index))` fail on inputs where `sort_index()` works?

Overview: Implement a pandas function that reorders a DataFrame's rows by their index labels in ascending order. It tests how sort_index works, how to keep the original labels without a reset, and how to return a new DataFrame without mutating the input.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement def sort_by_index(df: pd.DataFrame) -> pd.DataFrame that returns the rows reordered by row index labels ascending using df.sort_index(). The index may be an unsorted integer index.

The function must also meet two requirements:

  • Preserve the original index values: no reset_index() or relabeling.
  • Do not mutate the input df : return a new DataFrame.

Example:

df = pd.DataFrame({"a": [10, 20, 30]}, index=[2, 0, 1])
sort_by_index(df)
#     a
# 0  20
# 1  30
# 2  10

What a Strong Answer Covers Guidance

  • Orders the rows by their index labels with sort_index() in the default ascending direction.
  • Returns the new DataFrame without inplace=True , and can show that the input keeps its original order.
  • Keeps the original labels in the result, with no reset_index() and no ignore_index=True .
  • Handles edge cases: an empty frame, an index that is already sorted, duplicate labels and missing labels.

Follow-up Questions Guidance

  • How would you sort in descending order, or sort the columns by their labels?
  • If the index has duplicate labels, what order do the tied rows come out in, and how would you guarantee they keep their original relative order?
  • How does sort_index() behave on a MultiIndex, and how would you sort by only one level?
  • Why can df.reindex(sorted(df.index)) fail on inputs where sort_index() works?