Change language

Python | Pandas Dataframe.duplicated ()

| |

An important part of data analysis is analyzing duplicate values ​​ and removing them. The Pandas duplicated() method helps to analyze only duplicate values. It returns a logical series that is True for unique elements only.

Syntax :

 DataFrame.duplicated (subset = None, keep = ’first’) 

Options:

subset: Takes a column or list of column label. It’s default value is none. After passing columns, it will consider them only for duplicates.
keep: Controls how to consider duplicate value. It has only three distinct value and default is ’first’.
-"  If ’first’, it considers first value as unique and rest of the same values ​​as duplicate.
-"  If ’last’, it considers last value as unique and rest of the same values ​​as duplicate.
-"  If False, it consider all of the same values ​​as duplicates.

To download the CSV file you are using, click here.

Example # 1: Returning Boolean

In the following example boolean streak is returned based on duplicate values ​​in the Name column.

# import pandas package

import pandas as pd

 
# create data frame from CSV file

data = pd.read_csv ( "employees.csv" )

 
# sort by name

data.sort_values ​​( " First Name " , inplace = True )

 
# create bool series

bool_series = data [ "First Name " ]. Duplicated ()

  
# displaying data
data.head ()

 
# Display data
data [bool_series]

Output:
As shown in the output image since the keep parameter was a value on the mind by default, that is, "first", therefore whenever a name is encountered, the first is considered "Unique" and "res Duplicate".

Example # 2: removing duplicates
In this example, the keep parameter is set to False, so only unique values ​​are accepted, and duplicate values ​​are removed from the data.

# pandas package import

import pandas as pd

 
# create data frame from CSV file

data = pd.read_csv ( " employees.csv " )

  
# sort by name

data.sort_values ​​( " First Name " , inplace = True )

 
# create series bool

bool_series = data [ "First Name" ]. duplicated (keep = False )

 
# bool series
bool_series

 
# pass a NOT bool series to view only unique values ​​

data < / code> = data [~ bool_series]

 
# data display
data.info ()
data

Output:
Since the duplicated () method returns False for Duplicates, a NOT series is accepted to see the unique value in the data frame.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically