Pandas DataFrame | drop_duplicates method
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 |
|---|---|
| Keep only the first occurrence, and drop the rest. |
| Keep only the last occurrence, and drop the first. |
| 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 labels0,1, ...,n-1, wherenis 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 C0 2 4 21 2 5 82 2 4 2
The 1st and 3rd rows are duplicate. To remove duplicate rows:
df.drop_duplicates()
A B C0 2 4 21 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 C0 2 4 21 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 C0 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 C0 2 4 21 9 9 92 2 4 2
Keeping only the first occurrence
df.drop_duplicates(keep="first") # This is the default behaviour
A B C0 2 4 21 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 C1 9 9 92 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 C1 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 C0 2 4 21 9 9 92 2 4 2
We remove all duplicate rows with inplace=True:
df.drop_duplicates(inplace=True)df
A B C0 2 4 21 9 9 9
As shown in the output, the source DataFrame has been modified.