Pandas DataFrame drop_duplicates() Method


Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.

Feel free to view the PyCharm installation guide for the required libraries.

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame drop_duplicates()

The drop_duplicates() method returns a DataFrame/Series with duplicate rows removed.

The syntax for this method is as follows:

DataFrame.drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)
subsetSpecify the column(s) to locate the duplicates. By default, all columns.
keepDetermines what duplicates to keep, first or the last occurrence. By default, first.
inplaceIf False creates a copy of the DataFrame/Series. By default, False. If True, the original DataFrame/Series updates.
ignore_indexIf True, the returning axis will start the numbers from 0 – n value. By default, False.

For this example, Rivers Clothing has found a duplicate clothing line in the DataFrame. They need this duplicate removed. To perform this task, run the following code:

df = pd.DataFrame({'Tops':    [10.12, 12.23, 13.95],
                   'Tanks':   [11.35, 13.45, 14.98],
                   'Sweats':  [27.15, 21.85, 35.75],
                   'Pants':   [21.37, 56.99, 94.87],
                   'Sweats':  [27.15, 21.85, 35.75]})

result = df.drop_duplicates()


010.12 11.3527.1521.37
112.2313.4521.85 56.99
213.95 14.98  35.75 94.87

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.