Pandas DataFrame Duplicated Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the duplicated() method in the Pandas library for Python. The duplicated() method is used to find duplicate rows in a DataFrame.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/select_columns -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} python/booleans -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} python/lists -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} python/tuples -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} python/dictionaries -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} python/importing_modules -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} python/numerical_computing -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} python/data_analysis -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} python/build_in_functions -.-> lab-68615{{"`Pandas DataFrame Duplicated Method`"}} end

Import the necessary libraries

First, we need to import the pandas library as pd.

import pandas as pd

Create a DataFrame

Next, let's create a DataFrame to work with. We will use the following example DataFrame:

df = pd.DataFrame({'Name': ['Navya', 'Vindya', 'Navya', 'Vindya', 'Sinchana', 'Sinchana'],
                   'Skills': ['Python', 'Java', 'Python', 'Java', 'Java', 'Java']})

Find duplicated rows

To find duplicated rows in the DataFrame, we can use the duplicated() method. By default, it considers all columns for identifying duplicates. It returns a boolean Series where True represents duplicate rows and False represents unique rows.

duplicates = df.duplicated()
print(duplicates)

Specify columns for identifying duplicates

If we want to consider only certain columns for identifying duplicates, we can pass the column label(s) to the subset parameter of the duplicated() method.

duplicates_subset = df.duplicated(subset=['Skills'])
print(duplicates_subset)

Specify duplicate marking

The keep parameter of the duplicated() method determines how duplicates should be marked. By default, it is set to 'first', which marks all duplicates as True except for the first occurrence. We can also set it to 'last' or False to mark duplicates differently.

duplicates_keep_last = df.duplicated(keep='last')
print(duplicates_keep_last)

duplicates_keep_false = df.duplicated(keep=False)
print(duplicates_keep_false)

Summary

In this lab, we learned how to use the duplicated() method in Pandas to find duplicate rows in a DataFrame. We saw how to identify duplicates based on certain columns, specify duplicate marking, and obtain a boolean Series representing duplicate rows. The duplicated() method is a useful tool for data cleaning and identifying duplicated data.

Other Python Tutorials you may like