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.
An important part of Data analysis is analyzing Duplicate Values and removing them. Pandas
drop_duplicates() method helps in removing duplicates from the data frame.
Syntax: DataFrame.drop_duplicates(subset=None, keep=’first’, inplace=False)
subset: 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: keep is to control 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
inplace: Boolean values, removes rows with duplicates if True.
Return type: DataFrame with removed duplicate rows depending on Arguments passed.
To download the CSV file used, Click Here.
Example #1: Removing rows with same First Name
In the following example, rows having same First Name are removed and a new data frame is returned.
As shown in the image, the rows with same names were removed from data frame.
Example #2: Removing rows with all duplicate values
In this example, rows having all values will be removed. Since the csv file isn’t having such a row, a random row is duplicated and inserted in data frame first.
As shown in the output image, the length after removing duplicates is 999. Since the keep parameter was set to False, all of the duplicate rows were removed.
- Python | pandas.map()
- Python | Pandas.factorize()
- Python | Pandas dataframe.mad()
- Python | Pandas dataframe.max()
- Python | Pandas Series.str.pad()
- Python | Pandas Panel.mul()
- Python | Pandas Series.dt.day
- Python | Pandas Series.std()
- Python | Pandas.melt()
- Python | Pandas PeriodIndex.day
- Python | Pandas dataframe.mean()
- Python | Pandas Series.agg()
- Python | Pandas dataframe.div()
- Python | Pandas Series.dt.second
- Python | Pandas.pivot()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.