DataFrame customers
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| customer_id | int |
| name | object |
| email | object |
+-------------+--------+
이 데이터프레임에는 이메일 열을 기준으로 중복된 행들이 있습니다.
이 중복된 행들을 제거하고 첫 번째 발생한 행만 남기는 솔루션을 작성하세요.
결과 형식은 다음 예시와 같습니다.
Example 1:
Input:
+-------------+---------+---------------------+
| customer_id | name | email |
+-------------+---------+---------------------+
| 1 | Ella | emily@example.com |
| 2 | David | michael@example.com |
| 3 | Zachary | sarah@example.com |
| 4 | Alice | john@example.com |
| 5 | Finn | john@example.com |
| 6 | Violet | alice@example.com |
+-------------+---------+---------------------+
Output:
+-------------+---------+---------------------+
| customer_id | name | email |
+-------------+---------+---------------------+
| 1 | Ella | emily@example.com |
| 2 | David | michael@example.com |
| 3 | Zachary | sarah@example.com |
| 4 | Alice | john@example.com |
| 6 | Violet | alice@example.com |
+-------------+---------+---------------------+
설명: Alic (customer_id = 4)과 Finn (customer_id = 5)은 모두 john@example.com을 사용하므로, 이 이메일의 첫 번째 발생만 유지됩니다.
✏️ 풀이
import pandas as pd
def dropDuplicateEmails(customers: pd.DataFrame) -> pd.DataFrame:
return customers.drop_duplicates(subset='email', keep='first')
📚 개념정리
drop_duplicates() ? 중복되는 행을 제거하는 메서드
df.drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)
subset : 중복값을 제거할 열
keep : 중복제거 시 남길 행 / first : 첫번째 값 / last : 마지막 값
inplace : 원본 변경 여부
ignore_index : 원래의 index를 무시할지 여부
'[코딩테스트] > [Pandas]' 카테고리의 다른 글
[LeetCode - Pandas] (Easy) 2880. Select Data (0) | 2025.04.01 |
---|---|
[LeetCode - Pandas] (Easy) 2881. Create a New Column (2) | 2025.04.01 |
[LeetCode - Pandas] (Easy) 2883. Drop Missing Data (0) | 2025.03.31 |
[LeetCode - Pandas] (Easy) 2884. Modify Columns (0) | 2025.03.30 |
[LeetCode - Pandas] (Easy) 2885. Rename Columns (0) | 2025.03.30 |