Deduplicate DataFrame by Index Label

Quick Overview

Write a pandas function that removes rows with repeated index labels, keeping the first row for each label while preserving the original labels, row order and columns. It tests index-based deduplication with boolean masks and how that differs from removing rows with duplicate values.

Deduplicate DataFrame by Index Label

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `def drop_duplicate_index_labels(df: pd.DataFrame) -> pd.DataFrame` that removes rows so the index has unique labels, keeping the first occurrence of each repeated label. Requirements: - Keep the first row per label and drop later repeats. - Preserve the original index labels: do not call `reset_index()`. - Leave columns and their order unchanged. - The result must satisfy `result.index.is_unique == True`. Example: ```python df = pd.DataFrame({"v": [1, 2, 3, 4]}, index=["a", "b", "a", "c"]) drop_duplicate_index_labels(df) # v # a 1 # b 2 # c 4 # result.index.is_unique -> True ``` ```hint Where the repeats are The repeats are in the index labels. Check which part of the frame `drop_duplicates` compares before you use it, and look at what the index object itself can tell you about repeated values. ``` ### What a Strong Answer Covers - Which pandas operation detects repeated index labels, and why value-based deduplication answers a different question - How the first occurrence is kept while labels, row order and columns stay unchanged - Time and space cost of the approach - Edge cases such as an empty frame, repeated NaN labels and a MultiIndex ### Follow-up Questions - How would you keep the last row per index label instead? - What if you want to drop all rows whose label appears more than once? - How would you keep, for each repeated label, the row with the latest value in a timestamp column?

Overview: Write a pandas function that removes rows with repeated index labels, keeping the first row for each label while preserving the original labels, row order and columns. It tests index-based deduplication with boolean masks and how that differs from removing rows with duplicate values.

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

Write def drop_duplicate_index_labels(df: pd.DataFrame) -> pd.DataFrame that removes rows so the index has unique labels, keeping the first occurrence of each repeated label.

Requirements:

  • Keep the first row per label and drop later repeats.
  • Preserve the original index labels: do not call reset_index() .
  • Leave columns and their order unchanged.
  • The result must satisfy result.index.is_unique == True .

Example:

df = pd.DataFrame({"v": [1, 2, 3, 4]}, index=["a", "b", "a", "c"])

drop_duplicate_index_labels(df)
#    v
# a  1
# b  2
# c  4
# result.index.is_unique -> True

What a Strong Answer Covers Guidance

  • Which pandas operation detects repeated index labels, and why value-based deduplication answers a different question
  • How the first occurrence is kept while labels, row order and columns stay unchanged
  • Time and space cost of the approach
  • Edge cases such as an empty frame, repeated NaN labels and a MultiIndex

Follow-up Questions Guidance

  • How would you keep the last row per index label instead?
  • What if you want to drop all rows whose label appears more than once?
  • How would you keep, for each repeated label, the row with the latest value in a timestamp column?