Drop All Rows With Conflicting IDs

Quick Overview

Write a pandas function that removes every row whose id appears more than once, keeping only rows with a unique id in their original order with a fresh index. It tests duplicate detection with the keep options, index handling and edge cases such as an empty result or missing ids.

Drop All Rows With Conflicting IDs

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def drop_all_conflicting_ids(df: pd.DataFrame) -> pd.DataFrame` where `df` has columns `['id', 'value']`. Remove every row whose `id` appears in more than one row, keeping only rows with a unique `id`. Requirements: - Drop all copies of a repeated `id`, not just the later ones. - Keep the original relative order of surviving rows. - Reset the index to 0 to n-1. - Do not mutate the input: return a new frame. - If all ids conflict, return an empty frame with the same columns. Example: ```python df = pd.DataFrame({ 'id': [1, 2, 2, 3, 4, 4], 'value': ['a','b','c','d','e','f'], }) drop_all_conflicting_ids(df) # id value # 0 1 a # 1 3 d ``` ```hint Which copies get flagged Duplicate checks in pandas can flag only the later copies of a repeated value or every copy of it. Decide which one this task needs, and check what the index looks like after you filter. ``` ### What a Strong Answer Covers - How every copy of a repeated id gets flagged, including the first one - Keeping the surviving rows in input order and relabeling the index without adding a column - Leaving the input unmodified, and returning the right columns when no row survives - Time and space cost, and how missing or inconsistently formatted ids behave ### Follow-up Questions - How would you keep the `value` list per conflicting id for logging? - How does `keep=False` differ from `keep='first'` here? - What if rows that repeat an id with the same value should count as one row, so only ids with different values are conflicts?

Overview: Write a pandas function that removes every row whose id appears more than once, keeping only rows with a unique id in their original order with a fresh index. It tests duplicate detection with the keep options, index handling and edge cases such as an empty result or missing ids.

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

Implement def drop_all_conflicting_ids(df: pd.DataFrame) -> pd.DataFrame where df has columns ['id', 'value']. Remove every row whose id appears in more than one row, keeping only rows with a unique id.

Requirements:

  • Drop all copies of a repeated id , not just the later ones.
  • Keep the original relative order of surviving rows.
  • Reset the index to 0 to n-1.
  • Do not mutate the input: return a new frame.
  • If all ids conflict, return an empty frame with the same columns.

Example:

df = pd.DataFrame({
    'id':    [1, 2, 2, 3, 4, 4],
    'value': ['a','b','c','d','e','f'],
})

drop_all_conflicting_ids(df)
#    id value
# 0   1     a
# 1   3     d

What a Strong Answer Covers Guidance

  • How every copy of a repeated id gets flagged, including the first one
  • Keeping the surviving rows in input order and relabeling the index without adding a column
  • Leaving the input unmodified, and returning the right columns when no row survives
  • Time and space cost, and how missing or inconsistently formatted ids behave

Follow-up Questions Guidance

  • How would you keep the value list per conflicting id for logging?
  • How does keep=False differ from keep='first' here?
  • What if rows that repeat an id with the same value should count as one row, so only ids with different values are conflicts?