Pandas DataFrame duplicated() Method | Pandas Method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas duplicated() method identifies duplicated rows in a DataFrame. It returns a boolean series which is True only for unique rows.
Example:
Python3
import pandas as pd
df = pd.DataFrame({
'Name' : [ 'Alice' , 'Bob' , 'Alice' , 'Charlie' ],
'Age' : [ 25 , 32 , 25 , 37 ]
})
duplicates = df[df.duplicated()]
print (duplicates)
|
Output:
Name Age
2 Alice 25
SyntaxÂ
Syntax: DataFrame.duplicated(subset=None, keep=’first’)
Parameters:Â Â
- 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 values and the default is ‘first’.Â
–> If ‘first‘, it considers the first value as unique and the rest of the same values as duplicate.Â
–> If ‘last‘, it considers the last value as unique and the rest of the same values as duplicate.Â
–> If False, it considers all of the same values as duplicates.Â
Returns: A series with boolean values for each row in the DataFrameÂ
To download the CSV file used, Click Here.
Examples
Let’s look at some examples of the duplicated method in Pandas library used to identify duplicated rows in a DataFrame.
Example 1: Returning a boolean series
In the following example, a boolean series is returned based on duplicate values in the First Name column.Â
Python
import pandas as pd
data = pd.read_csv( "employees.csv" )
data.sort_values( "First Name" , inplace = True )
bool_series = data[ "First Name" ].duplicated()
data.head()
data[bool_series]
|
Output:Â
As shown in the output image, since the keep parameter was a default that is ‘first‘, hence whenever the name occurs, the first one is considered Unique, and the rest Duplicate.Â
Â
Example 2: Removing duplicatesÂ
In this example, the keep parameter is set to False, so that only Unique values are taken and the duplicate values are removed from DataFrame.Â
Python
import pandas as pd
data = pd.read_csv( "employees.csv" )
data.sort_values( "First Name" , inplace = True )
bool_series = data[ "First Name" ].duplicated(keep = False )
bool_series
data = data[~bool_series]
data.info()
data
|
Output:Â
Since the duplicated() method returns False for duplicates, the NOT of the series is taken to see the unique values in the DataFrame.Â
Â
Last Updated :
02 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...