search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | drop_duplicates method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas's DataFrame.drop_duplicates(~) method returns a DataFrame with duplicate rows removed.

Parameters

1. subset | string or list | optional

The columns used to identify duplicates. By default, all columns are used.

2. keep | string or boolean | optional

How to deal with duplicate rows:

Value

Meaning

"first"

Keep only the first occurrence, and drop the rest.

"last"

Keep only the last occurrence, and drop the first.

False

Drop all duplicates.

By default, keep="first".

3. inplace | boolean | optional

  • If True, then the method will directly modify the source DataFrame instead of creating a new DataFrame.

  • If False, then a new DataFrame will be created and returned.

By default, inplace=False.

4. ignore_index | boolean | optional

  • If True, then the returned DataFrame will have index labels 0, 1, ..., n-1, where n is the number of rows of the returned DataFrame.

  • If False, then the returned DataFrame will keep its original index.

By default, ignore_index=False.

Return Value

A DataFrame with duplicate rows removed according to the specified parameters. If inplace=True, then nothing is returned since the source DataFrame is directly modified.

Examples

Basic Usage

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,2,2], "B":[4,5,4], "C":[2,8,2]})
df
A B C
0 2 4 2
1 2 5 8
2 2 4 2

The 1st and 3rd rows are duplicate. To remove duplicate rows:

df.drop_duplicates()
A B C
0 2 4 2
1 2 5 8

Removing duplicate rows, with only certain columns considered

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,2], "B":[4,3], "C":[2,2]})
df
A B C
0 2 4 2
1 2 3 2

Here, the 1st and 2nd rows are not exactly a duplicate ([2,4,2] vs [2,3,2]). We can still declare them as a duplicate by specifying subset=["A","C"], which will only consider columns A and C during the check for duplicates:

df.drop_duplicates(subset=["A","C"])
A B C
0 2 4 2

Different ways of dealing with duplicates

There are three ways in which we can deal with duplicates, as specified by keep="first" || "last" || "False".

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,9,2], "B":[4,9,4], "C":[2,9,2]})
df
A B C
0 2 4 2
1 9 9 9
2 2 4 2

Keeping only the first occurrence

df.drop_duplicates(keep="first") # This is the default behaviour
A B C
0 2 4 2
1 9 9 9

Notice how the first occurrence of the duplicate is left as is.

Keeping only the last occurrence

df.drop_duplicates(keep="last")
A B C
1 9 9 9
2 2 4 2

Notice how the last occurrence of the duplicate is left as is.

Removing all occurrences

df.drop_duplicates(keep=False)
A B C
1 9 9 9

Notice how the all the duplicates got removed.

Removing duplicate rows in-place

To remove duplicate rows in-place, set inplace=True. This will directly remove the duplicate rows from the source DataFrame instead of creating a new one.

Consider the following DataFrame;

df = pd.DataFrame({"A":[2,9,2], "B":[4,9,4], "C":[2,9,2]})
df
A B C
0 2 4 2
1 9 9 9
2 2 4 2

We remove all duplicate rows with inplace=True:

df.drop_duplicates(inplace=True)
df
A B C
0 2 4 2
1 9 9 9

As shown in the output, the source DataFrame has been modified.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...