How can you sort a pandas DataFrame or a Series?

Quick Overview

Asks how to sort a pandas DataFrame or Series by values or by index labels. Tests working knowledge of the pandas sorting API, including multi-column sorts with mixed directions, missing values, custom orders and what happens to the index after sorting.

How can you sort a pandas DataFrame or a Series?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

In pandas, how can you sort a `DataFrame` or a `Series`? ```hint Two things to sort by A pandas object can be ordered by more than one thing. Think about what those are, and what changes when you sort a DataFrame by several columns at once. ``` ### What a Strong Answer Covers - The methods for sorting by values and by index labels, for both a Series and a DataFrame. - Sorting by several columns, including a different direction for each column. - The options that control direction, missing values, and whether the original object changes. - What happens to the index and to tied rows after a sort. ### Follow-up Questions - How would you sort a column of names case-insensitively? - How do you sort a column by a custom order such as small, medium, large? - You only need the 5 largest values of a column in a 10-million-row DataFrame. Is a full sort the best option? - Why does `df = df.sort_values('age', inplace=True)` leave `df` set to `None`?

Overview: Asks how to sort a pandas DataFrame or Series by values or by index labels. Tests working knowledge of the pandas sorting API, including multi-column sorts with mixed directions, missing values, custom orders and what happens to the index after sorting.

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

In pandas, how can you sort a DataFrame or a Series?

What a Strong Answer Covers Guidance

  • The methods for sorting by values and by index labels, for both a Series and a DataFrame.
  • Sorting by several columns, including a different direction for each column.
  • The options that control direction, missing values, and whether the original object changes.
  • What happens to the index and to tied rows after a sort.

Follow-up Questions Guidance

  • How would you sort a column of names case-insensitively?
  • How do you sort a column by a custom order such as small, medium, large?
  • You only need the 5 largest values of a column in a 10-million-row DataFrame. Is a full sort the best option?
  • Why does df = df.sort_values('age', inplace=True) leave df set to None ?