problemeasyalgorithmsleetcode-2882leetcode 2882leetcode2882

Drop Duplicate Rows

EasyUpdated: Aug 2, 2025
Practice on:

Problem

DataFrame customers +-------------+--------+ | Column Name | Type | +-------------+--------+ | customer_id | int | | name | object | | email | object | +-------------+--------+

There are some duplicate rows in the DataFrame based on the email column.

Write a solution to remove these duplicate rows and keep only the first occurrence.

The result format is in the following example.

Examples

Example 1

Input:
+-------------+---------+---------------------+
| customer_id | name    | email               |
+-------------+---------+---------------------+
| 1           | Ella    | [email protected]   |
| 2           | David   | [email protected] |
| 3           | Zachary | [email protected]   |
| 4           | Alice   | [email protected]    |
| 5           | Finn    | [email protected]    |
| 6           | Violet  | [email protected]   |
+-------------+---------+---------------------+
Output: 
+-------------+---------+---------------------+
| customer_id | name    | email               |
+-------------+---------+---------------------+
| 1           | Ella    | [email protected]   |
| 2           | David   | [email protected] |
| 3           | Zachary | [email protected]   |
| 4           | Alice   | [email protected]    |
| 6           | Violet  | [email protected]   |
+-------------+---------+---------------------+
Explanation:
Alic (customer_id = 4) and Finn (customer_id = 5) both use [email protected], so only the first occurrence of this email is retained.

## Solution

### Method 1 – pandas drop_duplicates

#### Intuition

To remove duplicate rows based on the `email` column and keep only the first occurrence, we can use the `drop_duplicates` method in pandas, specifying the `email` column and `keep='first'`.

#### Approach

1. Use `drop_duplicates` on the DataFrame, specifying `subset=['email']` and `keep='first'`.
2. Return the resulting DataFrame, sorted by `customer_id` if required.

#### Code

{{< code_tabs >}}
##### Python
```python
import pandas as pd

def drop_duplicate_rows(customers: pd.DataFrame) -> pd.DataFrame:
    return customers.drop_duplicates(subset=['email'], keep='first')

Complexity

  • ⏰ Time complexity: O(n), where n is the number of rows in the DataFrame.
  • 🧺 Space complexity: O(n), for the output DataFrame.

Comments