Pandas Duplicate Data Handling
Duplicate data is a common problem in data analysis that affects the accuracy of statistical results. Pandas provides complete functionality for detecting and handling duplicate data.
Detect duplicate data
The duplicated method
Example
import
pandas
as
pd
Create a DataFrame containing duplicate rows
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
,
"Zhang San"
,
"Li Si"
,
"Zhao Liu"
]
,
"Age"
:
[
25
,
30
,
28
,
25
,
30
,
35
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
,
"Beijing"
,
"Shenzhen"
,
"Beijing"
]
}
)
(
"Original data:"
)
(
df
)
(
)
Detect duplicate rows (by default, marking starts with the first occurrence, keep='first')
(
"Detect duplicate rows:"
)
(
df.
duplicated
(
)
)
(
)
Count the number of duplicate rows
(
f
"Number of duplicate rows: {df.duplicated().sum()}"
)
(
)
Detect duplicates in specific columns
(
"Detect duplicates by the 'name' column:"
)
(
df.
duplicated
(
subset
=
[
"Name"
]
)
)
The keep parameter
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Zhang San"
,
"Wang Wu"
,
"Li Si"
]
,
"Age"
:
[
25
,
30
,
25
,
28
,
30
]
}
)
(
"Original data:"
)
(
df
)
(
)
keep='first': keep the first occurrence (default)
(
"keep='first' (default):"
)
(
df.
duplicated
(
keep
=
"first"
)
)
(
)
keep='last': keep the last occurrence
(
"keep='last':"
)
(
df.
duplicated
(
keep
=
"last"
)
)
(
)
keep=False: mark all duplicates (keep none)
(
"keep=False (mark all duplicates):"
)
(
df.
duplicated
(
keep
=
False
)
)
Remove duplicate data
Basic usage of drop_duplicates
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Zhang San"
,
"Wang Wu"
,
"Li Si"
]
,
"Age"
:
[
25
,
30
,
25
,
28
,
30
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Beijing"
,
"Guangzhou"
,
"Shenzhen"
]
}
)
(
"Original data:"
)
(
df
)
(
)
Remove duplicate rows (by default, keep the first)
(
"Remove duplicate rows (keep the first):"
)
(
df.
drop_duplicates
(
)
)
(
)
Keep the last one
(
"Remove duplicate rows (keep the last):"
)
(
df.
drop_duplicates
(
keep
=
"last"
)
)
(
)
Keep no duplicate rows
(
"Remove all duplicate rows:"
)
(
df.
drop_duplicates
(
keep
=
False
)
)
Remove duplicates by column
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Zhang San"
,
"Wang Wu"
]
,
"Age"
:
[
25
,
30
,
35
,
28
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Beijing"
,
"Guangzhou"
]
}
)
(
"Original data:"
)
(
df
)
(
)
Deduplicate by a single column
(
"Deduplicate by the 'name' column:"
)
(
df.
drop_duplicates
(
subset
=
[
"Name"
]
)
)
(
)
Deduplicate by a combination of columns
(
"Deduplicate by the 'name'+'city' combination:"
)
(
df.
drop_duplicates
(
subset
=
[
"Name"
,
"City"
]
)
)
(
)
Keep the row with the largest value in a specific column
(
"Group by 'name' and keep the row with the greatest age:"
)
(
df.
sort_values
(
"Age"
,
ascending
=
False
)
.
drop_duplicates
(
subset
=
[
"Name"
]
,
keep
=
"first"
)
)
Practical example: data cleaning
Comprehensive example
Example
import
pandas
as
pd
import
numpy
as
np
Create simulated order data (including duplicates)
orders
=
pd.
DataFrame
(
{
"order_id"
:
[
"O001"
,
"O002"
,
"O001"
,
"O003"
,
"O002"
,
"O004"
]
,
"Customer ID"
:
[
"C001"
,
"C002"
,
"C001"
,
"C003"
,
"C002"
,
"C004"
]
,
"product"
:
[
"iPhone"
,
"MacBook"
,
"iPhone"
,
"iPad"
,
"AirPods"
,
"Apple Watch"
]
,
"amount"
:
[
8000
,
12000
,
8000
,
5000
,
1500
,
3000
]
,
"date"
:
[
"2024-01-01"
,
"2024-01-02"
,
"2024-01-01"
,
"2024-01-03"
,
"2024-01-02"
,
"2024-01-04"
]
}
)
(
"Original order data:"
)
(
orders
)
(
)
1. Detect duplicate orders
(
"Duplicate order detection:"
)
duplicates
=
orders
[
orders.
duplicated
(
subset
=
[
"order_id"
]
,
keep
=
False
)
]
(
duplicates
)
(
)
2. Remove completely duplicate orders
orders_clean
=
orders.
drop_duplicates
(
)
(
"After removing complete duplicates:"
)
(
f
"Original {len(orders)} rows -> {len(orders_clean)} rows"
)
(
)
3. Deduplicate by order_id, keeping the latest
Assume the last entry is the latest
orders_clean
=
orders.
sort_values
(
"date"
)
.
drop_duplicates
(
subset
=
[
"order_id"
]
,
keep
=
"last"
)
.
sort_index
(
)
(
"Deduplicate by order_id (keep the latest):"
)
(
orders_clean
)
Advanced usage
Keep specific rows by group
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"customer"
:
[
"A"
,
"A"
,
"A"
,
"B"
,
"B"
,
"B"
]
,
"Month"
:
[
"01"
,
"02"
,
"03"
,
"01"
,
"02"
,
"03"
]
,
"spending"
:
[
100
,
200
,
150
,
300
,
250
,
280
]
}
)
(
"Original data:"
)
(
df
)
(
)
Keep the month with the highest spending for each customer
(
"The month with the highest spending for each customer:"
)
result
=
df.
sort_values
(
"spending"
,
ascending
=
False
)
.
drop_duplicates
(
subset
=
[
"customer"
]
,
keep
=
"first"
)
.
sort_values
(
"customer"
)
(
result
)
(
)
Keep the first record for each customer (by month)
(
"The first record for each customer:"
)
result
=
df.
sort_values
(
"Month"
)
.
drop_duplicates
(
subset
=
[
"customer"
]
,
keep
=
"first"
)
(
result
)
Mark duplicates instead of removing them
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Zhang San"
,
"Wang Wu"
,
"Li Si"
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Beijing"
,
"Guangzhou"
,
"Shenzhen"
]
}
)
Add a duplicate-mark column
df
[
"is_duplicate"
]
=
df.
duplicated
(
subset
=
[
"Name"
]
,
keep
=
False
)
(
"Mark duplicate rows:"
)
(
df
)
(
)
Or use groupby to mark
df
[
"repeat_count"
]
=
df.
groupby
(
"Name"
)
[
"Name"
]
.
transform
(
"count"
)
df
[
"is_first"
]
=
~
df.
duplicated
(
subset
=
[
"Name"
]
,
keep
=
"first"
)
(
"Count repeats by group:"
)
(
df
)
Notes
1. Distinguish "complete duplicates" from "partial duplicates"
A complete duplicate means all column values are the same, while a partial duplicate means specific columns are the same. Use the subset parameter to specify the columns used to judge duplicates.
2. Choosing a retention strategy
Choose to keep the first, the last, or none according to business needs. Which one you keep may affect later analysis results.
3. The effect of data types
Data types are considered when judging duplicates; for example, the integer 1 and the float 1.0 are treated as different.
Before removing duplicate data, it is recommended to first analyze the causes and distribution of the duplicates to ensure that the removal does not lose important information.