Pandas df.drop_duplicates() Function
df.drop_duplicates() is a function in Pandas used to remove duplicate rows.
During data collection and integration, duplicate data is a common problem.drop_duplicates() can help you identify and remove duplicate rows based on specified columns or all columns, keeping the first or last occurrence. This is very useful for deduplicating data and ensuring data uniqueness.
Basic syntax and parameters
drop_duplicates() is a member function of DataFrame, called via the dot operator . to call it.
Syntax format
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)
Parameter Description
| Parameters | Type | Required | Description | Default value |
|---|---|---|---|---|
| subset | column label or list | Optional | Specifies the columns used to identify duplicates. If None, all columns are used. It can be a single column name or a list of column names. |
None |
| keep | str or False | Optional | Specifies which duplicate record to keep.'first' Keep the first occurrence;'last' Keep the last occurrence;False Remove all duplicate records. |
'first' |
| inplace | bool | Optional | If it is True, modify the original DataFrame directly without returning a new object; if False, a new DataFrame is returned and the original data is unchanged. |
False |
| ignore_index | bool | Optional | If it is True, reset the index of the result, numbering from 0; if False, the original index is preserved. |
False |
Return value description
- Returns a new DataFrame (if
inplace=False), orNone(ifinplace=True)。 - The returned DataFrame contains no duplicate rows.
Example
Let us thoroughly master drop_duplicates() through a series of examples from simple to complex.
Example 1: Remove completely duplicate rows
By default, all columns are used to determine whether a row is a duplicate.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame with duplicate rows
data
=
{
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Zhang San'
,
'Wang Wu'
,
'Li Si'
]
,
'Age'
:
[
25
,
30
,
25
,
35
,
30
]
,
'Department'
:
[
'Ji shu' (a Chinese phrase)
,
'Marketing'
,
'Ji shu' (a Chinese phrase)
,
'Ji shu' (a Chinese phrase)
,
'Marketing'
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Remove duplicate rows, keeping the first occurrence
df_cleaned
=
df.
drop_duplicates
(
)
(
"Data after removing duplicate rows:"
)
(
df_cleaned
)
Expected output:
原始数据:
姓名 年龄 部门
0 张三 25 技术
1 李四 30 市场
2 张三 25 技术
3 王五 35 技术
4 李四 30 市场
==================================================
Data after deleting duplicate lines:
0 姓名 年龄 部门
0 张三 25 技术
1 李四 30 市场
3 王五 35 技术
Code analysis:
- In the original data, row 0 and row 2 are identical (Zhang San, 25 years old, Technology Department).
- Row 1 and row 4 are also identical (Li Si, 30 years old, Marketing Department).
- Use
drop_duplicates()With the default parameters, duplicate rows are removed and the first occurrence is kept.
Example 2: Remove duplicate rows based on specified columns
You can use the subset The parameter determines duplicates based only on specific columns.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame
data
=
{
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Zhang San'
,
'Wang Wu'
]
,
'Age'
:
[
25
,
30
,
28
,
35
]
,
Zhang San appears twice, but with different ages
'Department'
:
[
'Ji shu' (a Chinese phrase)
,
'Marketing'
,
'Ji shu' (a Chinese phrase)
,
'Marketing'
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Determine duplicates based only on the "Name" column
df_cleaned
=
df.
drop_duplicates
(
subset
=
[
'Name'
]
)
(
"Data after deduplication by the Name column:"
)
(
df_cleaned
)
(
"="
50
)
Determine duplicates based on the "Name" and "Department" columns
df_cleaned2
=
df.
drop_duplicates
(
subset
=
[
'Name'
,
'Department'
]
)
(
"Data after deduplication by the Name and Department columns:"
)
(
df_cleaned2
)
Expected output:
原始数据:
姓名 年龄 部门
0 张三 25 技术
1 李四 30 市场
2 张三 28 技术 # 虽然姓名重复,但年龄不同
3 王五 35 市场
==================================================
Delete duplicate data based on the name column:
姓名 年龄 部门
0 张三 25 技术 # 保留第一条张三的记录
2 张三 28 技术
3 王五 35 市场
==================================================
Delete duplicate data based on the Name and Department columns:
参数
姓名 年龄 部门
0 张三 25 技术
1 李四 30 市场
3 王五 35 市场
Code analysis:
- When determining duplicates by the Name column, rows 0 and 2 both have the name "Zhang San" and are considered duplicates, so only row 0 is kept.
- When determining duplicates by the Name and Department columns, rows 0 and 2 have the same name and department, so row 0 is kept.
Example 3: Keep the last duplicate record
Use keep='last' The parameter can keep the last occurrence of duplicate records.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame with duplicate rows
data
=
{
'Student ID'
:
[
'S001'
,
'S002'
,
'S001'
,
'S003'
,
'S002'
]
,
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Zhang San'
,
'Wang Wu'
,
'Li Si'
]
,
'Score'
:
[
85
,
90
,
88
,
92
,
85
]
For the same student ID, scores may differ
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Keep the first record (default)
df_first
=
df.
drop_duplicates
(
subset
=
[
'Student ID'
]
,
keep
=
'first'
)
(
"Keep the first duplicate record:"
)
(
df_first
)
(
"="
50
)
Keep the last record
df_last
=
df.
drop_duplicates
(
subset
=
[
'Student ID'
]
,
keep
=
'last'
)
(
"Keep the last duplicate record:"
)
(
df_last
)
Expected output:
原始数据:
学号 姓名 成绩
0 S001 张三 85
1 S002 李四 90
2 S001 张三 88 # 同一个人,成绩不同
3 S王五 92
4 S002 李四 85 # 同一个人,成绩不同
==================================================
Keep the first duplicate record:
学号 姓名 成绩
0 S001 张三 85 # 保留第一个85分
1 S002 李四 90 # 保留第一个90分
3 王五 92
==================================================
Keep the last duplicate record:
学号 姓名 成绩
2 S001 张三 88 # 保留最后一个88分
3 王五 92
4 S002 李四 85 # 保留最后一个85分
Code analysis:
- Student S001 has two records, with scores of 85 and 88, using
keep='first'keeps the one with 85,keep='last'keeps the one with 88. - Student S002 also has two records, with scores of 90 and 85.
- Choose whether to keep the first or the last based on business requirements.
Common Pandas functions