How do you sort a pandas DataFrame by column values or by index?

Quick Overview

Asks how to sort a pandas DataFrame by one or more columns or by its index labels. It tests knowledge of sort_values and sort_index, per-column sort direction, missing value placement, custom category orders and what happens to the index after sorting.

How do you sort a pandas DataFrame by column values or by index?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How will you sort a pandas `DataFrame`? Explain the methods you would use and the options that control the resulting order. ```hint Beyond one column Think about sorting by several columns at once, each in its own direction, and about where rows with missing values should end up. ``` ### What a Strong Answer Covers - The method for sorting by column values and the method for sorting by labels - Sorting by several columns with a different direction for each - How missing values are placed and how to change that - What happens to the index after sorting - At least one less common need, such as a custom category order, case-insensitive sorting or keeping ties in their current order ### Follow-up Questions - How would you sort a column of T-shirt sizes in the order S, M, L, XL? - How do you get the top 3 earners in each department? - Why might `df.loc[0]` and `df.iloc[0]` return different rows after sorting? - When does it matter whether the sort is stable?

Overview: Asks how to sort a pandas DataFrame by one or more columns or by its index labels. It tests knowledge of sort_values and sort_index, per-column sort direction, missing value placement, custom category orders and what happens to the index after sorting.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How will you sort a pandas DataFrame? Explain the methods you would use and the options that control the resulting order.

What a Strong Answer Covers Guidance

  • The method for sorting by column values and the method for sorting by labels
  • Sorting by several columns with a different direction for each
  • How missing values are placed and how to change that
  • What happens to the index after sorting
  • At least one less common need, such as a custom category order, case-insensitive sorting or keeping ties in their current order

Follow-up Questions Guidance

  • How would you sort a column of T-shirt sizes in the order S, M, L, XL?
  • How do you get the top 3 earners in each department?
  • Why might df.loc[0] and df.iloc[0] return different rows after sorting?
  • When does it matter whether the sort is stable?