Flag exact duplicate rows in a pandas DataFrame with keep='first'

Quick Overview

Write a pandas function that returns a boolean Series flagging every DataFrame row that exactly repeats an earlier row, with the first copy left unflagged. It tests keep='first' semantics, keeping the input index, dtype and name intact, and how missing values and index labels affect the comparison.

Flag exact duplicate rows in a pandas DataFrame with keep='first'

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `flag_duplicate_rows(df)` that returns a boolean `Series` marking each row of `df` that is an exact duplicate of an earlier row. Use `keep='first'` semantics: first occurrences and unique rows are `False`, and later exact repeats are `True`. Requirements: - A row counts as a duplicate only when every column matches an earlier row. - The result is a `Series` of dtype `bool`. - Preserve the input index exactly (no reset or reorder). - The Series `name` must be `None`. Example: ```python df = pd.DataFrame({"a": [1, 1, 2], "b": ["x", "x", "y"]}) flag_duplicate_rows(df) # 0 False # 1 True # 2 False # dtype: bool ``` ```hint Check the defaults pandas has a built-in that flags repeated rows. Look at its default `keep` value and confirm that what it returns still carries `df.index` before you write any loop. ``` ### What a Strong Answer Covers - Which pandas call produces the flags and why its default behavior matches `keep='first'` - How the result keeps the input index, the `bool` dtype and a `None` name, and which extra steps would break that - How missing values and index labels are treated when rows are compared - What happens with unhashable cells such as lists, and the cost on a large frame ### Follow-up Questions - How would you flag duplicates on a subset of columns, such as a user id and an event timestamp? - How do the flags change with `keep='last'` or `keep=False`, and when would you use each? - How would you produce the same flags without `duplicated()`, and how would your version handle rows that contain `NaN`?

Overview: Write a pandas function that returns a boolean Series flagging every DataFrame row that exactly repeats an earlier row, with the first copy left unflagged. It tests keep='first' semantics, keeping the input index, dtype and name intact, and how missing values and index labels affect the comparison.

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

Implement flag_duplicate_rows(df) that returns a boolean Series marking each row of df that is an exact duplicate of an earlier row. Use keep='first' semantics: first occurrences and unique rows are False, and later exact repeats are True.

Requirements:

  • A row counts as a duplicate only when every column matches an earlier row.
  • The result is a Series of dtype bool .
  • Preserve the input index exactly (no reset or reorder).
  • The Series name must be None .

Example:

df = pd.DataFrame({"a": [1, 1, 2], "b": ["x", "x", "y"]})
flag_duplicate_rows(df)
# 0    False
# 1     True
# 2    False
# dtype: bool

What a Strong Answer Covers Guidance

  • Which pandas call produces the flags and why its default behavior matches keep='first'
  • How the result keeps the input index, the bool dtype and a None name, and which extra steps would break that
  • How missing values and index labels are treated when rows are compared
  • What happens with unhashable cells such as lists, and the cost on a large frame

Follow-up Questions Guidance

  • How would you flag duplicates on a subset of columns, such as a user id and an event timestamp?
  • How do the flags change with keep='last' or keep=False , and when would you use each?
  • How would you produce the same flags without duplicated() , and how would your version handle rows that contain NaN ?