Return the Top K Rows of a Pandas DataFrame by a Column

Quick Overview

Return the k rows of a pandas DataFrame with the largest values in a numeric column, ordered from largest to smallest with ties kept in original row order. It tests nlargest, tie-breaking rules, index preservation, and the cost of selecting the top k compared with a full sort.

Return the Top K Rows of a Pandas DataFrame by a Column

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `top_k(df, col, k)` that returns the `k` rows with the largest values in numeric column `col`, ordered largest to smallest. Requirements: - Return exactly `k` rows, sorted by `col` descending. - Preserve all columns and each row's original index label. - Use `nlargest(k, col, keep='first')` so ties resolve by original position. Example: ```python df = pd.DataFrame({'name': ['a','b','c','d'], 'score': [10, 40, 25, 30]}) top_k(df, 'score', 2) # name score # 1 b 40 # 3 d 30 ``` ```hint Ties at the cutoff Picture two rows with the same score competing for the last of the k places. Decide which one should win and check that your call guarantees it. ``` ### Constraints & Assumptions - The frame has at least `k` rows. - `col` is numeric. - When values in `col` tie, the row that appears earlier in `df` ranks higher. ### What a Strong Answer Covers - Returns k rows in descending order of `col` with every column and the original index labels intact. - States the tie rule and why the chosen call follows it. - Compares the cost of selecting the top k with sorting the whole frame. - Says what happens to missing values in `col`. ### Follow-up Questions - How does `nlargest` differ from `sort_values(...).head(k)` in cost? - How would you break ties by a secondary column instead of position? - How would you return the top k rows within each group, for example the 3 highest scores per class?

Overview: Return the k rows of a pandas DataFrame with the largest values in a numeric column, ordered from largest to smallest with ties kept in original row order. It tests nlargest, tie-breaking rules, index preservation, and the cost of selecting the top k compared with a full sort.

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

Implement top_k(df, col, k) that returns the k rows with the largest values in numeric column col, ordered largest to smallest.

Requirements:

  • Return exactly k rows, sorted by col descending.
  • Preserve all columns and each row's original index label.
  • Use nlargest(k, col, keep='first') so ties resolve by original position.

Example:

df = pd.DataFrame({'name': ['a','b','c','d'], 'score': [10, 40, 25, 30]})
top_k(df, 'score', 2)
#   name  score
# 1    b     40
# 3    d     30

Constraints & Assumptions

  • The frame has at least k rows.
  • col is numeric.
  • When values in col tie, the row that appears earlier in df ranks higher.

What a Strong Answer Covers Guidance

  • Returns k rows in descending order of col with every column and the original index labels intact.
  • States the tie rule and why the chosen call follows it.
  • Compares the cost of selecting the top k with sorting the whole frame.
  • Says what happens to missing values in col .

Follow-up Questions Guidance

  • How does nlargest differ from sort_values(...).head(k) in cost?
  • How would you break ties by a secondary column instead of position?
  • How would you return the top k rows within each group, for example the 3 highest scores per class?