Removing duplicates is an essential skill to get accurate counts because you often don't want to count the same thing multiple times. In Python, this could be accomplished by using the Pandas module, which has a method known as drop_duplicates.
Let's understand how to use it with the help of a few examples.
Dropping Duplicate Names
Let's say you have a dataframe that contains vet visits, and the vet's office wants to know how many dogs of each breed have visited their office. However, there are dogs like Max and Stella, who have visited the vet more than once in your dataset. Hence, you cannot just count the number of each breed in the breed column.
To achieve this, you would remove rows that contain a dog name already listed earlier, or in other words, you will extract a dog with each name from the dataset once.
You would do this using the drop_duplicates method. It takes an argument subset, which is the column we want to find or duplicates based on - in this case, we want all the unique names.
date name breed weight_kg
02018-09-02 Bella Labrador 24.8712019-06-07 Max Chow Chow 24.0122019-03-19 Charlie Poodle 24.9532018-01-17 Stella Chihuahua 1.5142019-10-19 Lucy Chow Chow 24.0772019-03-30 Cooper Schnauzer 16.91102019-01-04 Bernie St. Bernard 74.98
In that case, we need to consider more than just name when dropping duplicates. Since Max and Max are different breeds, we can drop the rows with pairs of names and breeds listed earlier in the dataset.
date name breed weight_kg
02018-09-02 Bella Labrador 24.8712019-03-13 Max Chow Chow 24.1322019-03-19 Charlie Poodle 24.9532018-01-17 Stella Chihuahua 1.5142019-10-19 Lucy Chow Chow 24.0762019-06-07 Max Labrador 28.3572019-03-30 Cooper Schnauzer 16.91102019-01-04 Bernie St. Bernard 74.98
To base our duplicate dropping on multiple columns, we can pass a list of column names to the subset argument, in this case, name and breed.
Now both Max's have been included.
Interactive Example
In this exercise, you'll create some new DataFrames using unique values from sales. sales is available, and pandas is imported as pd.
You will perform the following steps:
First, you will remove rows of sales with duplicate pairs of store and type and save as store_types and print the head.
Then, you will remove rows of sales with duplicate pairs of store and department and save as store_depts and print the head.
Subset the rows that are holiday weeks, and drop the duplicate dates, saving as holiday_dates.
Finally, select the date column of holiday_dates, and print the holiday_dates dataframe.
# Drop duplicate store/type combinations
store_types = sales.drop_duplicates(subset=["store","type"])print(store_types.head())# Drop duplicate store/department combinations
store_depts = sales.drop_duplicates(subset=["store","department"])print(store_depts.head())# Subset the rows that are holiday weeks and drop duplicate dates
holiday_dates = sales[sales["is_holiday"]].drop_duplicates(subset="date")# Print date col of holiday_datesprint(holiday_dates["date"])
Explore data analysis with Python. Pandas DataFrames make manipulating your data easy, from selecting or replacing columns and indices to reshaping your data.
Removing duplicates is an essential skill to get accurate counts because you often don't want to count the same thing multiple times. In Python, this could be accomplished by using the Pandas module, which has a method known as
drop_duplicates.Let's understand how to use it with the help of a few examples.
Dropping Duplicate Names
Let's say you have a dataframe that contains vet visits, and the vet's office wants to know how many dogs of each breed have visited their office. However, there are dogs like Max and Stella, who have visited the vet more than once in your dataset. Hence, you cannot just count the number of each breed in the breed column.
To achieve this, you would remove rows that contain a dog name already listed earlier, or in other words, you will extract a dog with each name from the dataset once.
You would do this using the
drop_duplicatesmethod. It takes an argumentsubset, which is the column we want to find or duplicates based on - in this case, we want all the unique names.But, what if we have dogs with the same name?
Dropping Duplicate Pairs
In that case, we need to consider more than just
namewhen dropping duplicates. SinceMaxandMaxare different breeds, we can drop the rows with pairs of names and breeds listed earlier in the dataset.To base our duplicate dropping on multiple columns, we can pass a list of column names to the subset argument, in this case, name and breed.
Now both Max's have been included.
Interactive Example
In this exercise, you'll create some new DataFrames using unique values from
sales.salesis available, andpandasis imported aspd.You will perform the following steps:
saleswith duplicate pairs ofstoreandtypeand save asstore_typesand print the head.saleswith duplicate pairs ofstoreanddepartmentand save asstore_deptsand print the head.dates, saving asholiday_dates.datecolumn ofholiday_dates, and print theholiday_datesdataframe.When we run the above code, it produces the following result:
Try it for yourself.
To learn more about counting and aggregating data, please see this video from our course Data Manipulation with pandas.
This content is taken from DataCamp’s Data Manipulation with pandas course by Maggie Matsui and Richie Cotton.