How to delete duplicates from Pandas DataFrame in Python
Pandas is a popular data analysis library in Python, and it provides an efficient way to store and manipulate data in the form of a DataFrame. However, data can often contain duplicates, and it’s important to remove them to get accurate results. In this article, we’ll go over how to delete duplicates from a Pandas DataFrame in Python.
The first step is to create a DataFrame. You can do this by passing a dictionary or a list of lists to the pandas.DataFrame() function. For example, if you have a list of names and addresses, you can create a DataFrame by passing a list of dictionaries to the function:
import pandas as pd
data = [{"Name": "John", "Address": "123 Main St"},
{"Name": "Jane", "Address": "456 Elm St"},
{"Name": "John", "Address": "123 Main St"}]
df = pd.DataFrame(data)
In this example, the DataFrame contains a duplicate of the first record. To remove these duplicates, we use the drop_duplicates()
method of the DataFrame:
df = df.drop_duplicates()
By default, drop_duplicates()
removes all duplicates from the DataFrame, but you can also specify which columns to consider when checking for duplicates. For example, if you only want to consider the “Name” column:
df = df.drop_duplicates(subset="Name")
You can also specify multiple columns to consider by passing a list of column names to the subset
argument:
df = df.drop_duplicates(subset=["Name", "Address"])
In addition to removing duplicates, you can also specify how to handle duplicates using the keep
argument. By default, keep='first'
, which means that the first occurrence of a duplicate record is kept and the rest are removed. You can also specify keep='last'
to keep the last occurrence, or keep=False
to remove all duplicates.
In conclusion, removing duplicates from a Pandas DataFrame is a straightforward process. You can use the drop_duplicates()
method to remove all duplicates or specify which columns to consider when checking for duplicates. With these tools, you’ll be able to handle duplicates in your data and ensure that you’re getting accurate results.
Here is another example:
Let’s say you have a DataFrame that contains information about different fruits, including the type of fruit and the number of pieces available:
import pandas as pd
data = [{"Fruit": "Apple", "Pieces": 10},
{"Fruit": "Banana", "Pieces": 20},
{"Fruit": "Apple", "Pieces": 10},
{"Fruit": "Grape", "Pieces": 5}]
df = pd.DataFrame(data)
In this case, we have two records with the same information (type of fruit is “Apple” and pieces available is 10). To remove these duplicates, we can use the drop_duplicates()
method:
df = df.drop_duplicates()
If we want to consider only the “Fruit” column when checking for duplicates, we can specify the subset
argument:
df = df.drop_duplicates(subset="Fruit")
And if we want to keep only the last occurrence of a duplicate record, we can specify the keep
argument:
df = df.drop_duplicates(subset="Fruit", keep='last')
This will result in a DataFrame with only unique records:
Fruit Pieces
1 Banana 20
3 Grape 5
2 Apple 10
As you can see, removing duplicates from a Pandas DataFrame is simple and flexible, allowing you to consider specific columns and keep the desired occurrences of duplicates.
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.