Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Pandas DataFrame duplicated() Method

❮ DataFrame Reference


Example

Check which rows are duplicated and not:

import pandas as pd

data = {
  "name": ["John", "Mary", "John", "Sally", "Mary"],
  "age": [40, 30, 40, 50, 30],
  "city": ["Bergen", "Oslo", "Stavanger", "Oslo", "Oslo"]
}

df = pd.DataFrame(data)

s = df.duplicated()

print(s)
Try it Yourself »

Definition and Usage

The duplicated() method returns a Series with True and False values that describe which rows in the DataFrame are duplicated and not.

Use the subset parameter to specify which columns to include when looking for duplicates. By default all columns are included.

By default, the first occurrence of two or more duplicates will be set to False.

Set the keep parameter to False to also set the first occurrence to True.


Syntax

dataframe.duplicated(subset, keep)

Parameters

The parameters are keyword arguments.

Parameter Value Description
subset column label(s) Optional. A String, or a list, of the column names to include when looking for duplicates.  Default subset=None (meaning no subset is specified, and all columns should be included.
keep 'first'
'last'
False
Optional, default 'first'. Specifies how to deal with duplicates:
'first' means set the first occurrence to False, the rest to True.
'last' means set the last occurrence to False, the rest to True.
False means set all occurrences to True.

Return Value

A Series with a boolean value for each row in the DataFrame.


More Examples

Example

Only include the columns "name" and "age":

s = df.duplicated(subset=["name", "age"])

print(s)
Try it Yourself »

Example

Set all occurrences of duplicates to True:

s = df.duplicated(keep=False)

print(s)
Try it Yourself »

❮ DataFrame Reference

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.