Get the unique values (rows) of a dataframe in python Pandas

In this tutorial we will learn how to get the unique values (rows) of a dataframe in python pandas with drop_duplicates() function.  Lets see with an example

Create Dataframe:
import pandas as pd
import numpy as np

#Create a DataFrame
d = {
    'Name':['Alisa','Bobby','jodha','jack','raghu','Cathrine',
            'Alisa','Bobby','kumar','Alisa','Alex','Cathrine'],
    'Age':[26,24,23,22,23,24,26,24,22,23,24,24]
}

df = pd.DataFrame(d,columns=['Name','Age'])
print df

so the output will be

Get the unique values (rows) of a dataframe in python Pandas 1

 

Get the unique values (rows) of the dataframe in python pandas

drop_duplicates() function is used to get the unique values (rows) of the dataframe in python pandas.


# get the unique values (rows)
print df.drop_duplicates()

The above drop_duplicates() function removes all the duplicate rows and returns only unique rows. Generally it retains the first row when duplicate rows are present.

So the output will be

Get the unique values (rows) of a dataframe in python Pandas 2

 

Get the unique values (rows) of the dataframe in python pandas by retaining last row:


# get the unique values (rows) by retaining last row
print df.drop_duplicates(keep='last')

The above drop_duplicates() function with keep =’last’ argument,  removes all the duplicate rows and returns only unique rows by retaining the last row when duplicate rows are present.

So the output will be

Get the unique values (rows) of a dataframe in python Pandas 3

 

previous-small Get the unique values (rows) of a dataframe in python Pandas                                                                                                                 next_small Get the unique values (rows) of a dataframe in python Pandas