EasyNo. 2882

Drop Duplicate Rows — LeetCode 2882 Python Solution

Topics
Pandas
Reading time
2 min

Table schema

SQLTable schema
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.

Example

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

Python solution

PythonSolution
import pandas as pd


def dropDuplicateEmails(customers: pd.DataFrame) -> pd.DataFrame:
    return customers.drop_duplicates(subset=['email'])

Complexity

Time
O(n)
Space · auxiliary
O(1) to O(n)

Related problems

Frequently asked questions

How hard is LeetCode 2882. Drop Duplicate Rows?

LeetCode 2882. Drop Duplicate Rows is rated Easy on LeetCode.

What topics does LeetCode 2882. Drop Duplicate Rows cover?

LeetCode 2882. Drop Duplicate Rows is tagged Pandas on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.