Articles

get unique values from column pandas | drop_duplicates() & unique() in pandas with examples

get unique values from column pandas | drop_duplicates() & unique() in pandas with examples


In this pandas tutorial, we will discuss about:

  • different ways to get unique values from column pandas,
  • unique method in pandas,
  • unique pandas example,
  • drop_duplicates method in pandas,
  • drop_duplicates pandas example.

Lets first create one dataframe from where we will get unique values from column pandas.

 

DataFrame in pandas is two dimensional data structure that store data in 2-dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key.

 

Example: Create pandas dataframe

In this example, we will create dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":['1200','3422','5644','670']

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: Our dataframe is created

      college_id       college_name college_address Total Staff
one        c-001  vignan university          guntur        1200
two        c-021               vvit          guntur        3422
three      c-002           RVR - JC          guntur        5644
four       c-004  Andhra University          guntur         670

Now lets have a look at methods to get unique values from column pandas.


Get unique values from column pandas

We can use two methods to get unique values from column pandas:

  • unique method in pandas,
  • drop_duplicates method in pandas.

Method 1 : Using unique() in pandas

Here, we are using unique() in pandas to get the unique values. This method is applied with the column of the dataframe.

Syntax:

dataframe['column'].unique()
(or)
dataframe.column.unique()

Example 1: Method unique pandas example

In this unique pandas example, we will get get unique values from college_address column and Total Staff column.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":['1200','3422','5644','670']

                   },index=['one','two','three','four'])


# get unique values from college_address column
print(data['college_address'].unique())

print()

# get unique values from Total Staff column
print(data['Total Staff'].unique())

Outputunique pandas example result

['guntur']

['1200' '3422' '5644' '670']

From the above example,

  • the value - guntur from college_address column repeated 4 times. So the unique is only one - guntur and
  • from the Total Staff column all the values are unique.

Example 2unique pandas example 2

In this unique pandas example, we will get get unique values from college_address column.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":['1200','3422','5644','670']

                   },index=['one','two','three','four'])


# get unique values from college_address column
print(data.college_address.unique())

Outputunique pandas example result

['guntur']

From the above unique pandas example,

  • the value - guntur from college_address column repeated 4 times. so the unique prints only one value.

Method 2 : Using drop_duplicates() pandas

Here, we are using drop_duplicates method in pandas to get the unique values by removing the duplicated values. This method is applied with the column of the dataframe.

Syntax:

dataframe['column'].drop_duplicates()
(or)
dataframe.column.drop_duplicates()

Exampledrop_duplicates pandas example

In this drop_duplicates pandas example, we will get unique values from college_address column and Total Staff column.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":['1200','3422','5644','670']

                   },index=['one','two','three','four'])


# get unique values from college_address column
print(data['college_address'].drop_duplicates())

print()

# get unique values from Total Staff column
print(data['Total Staff'].drop_duplicates())

Outputdrop_duplicates pandas example result

one    guntur
Name: college_address, dtype: object

one      1200
two      3422
three    5644
four      670
Name: Total Staff, dtype: object

From the above drop_duplicates pandas example,

  • the value - guntur from college_address column repeated 4 times. so the unique is only one value and
  • from the Total Staff column all the values are unique. IE. duplicate values are removed.

Thus we have seen unique method in pandas and drop_duplicates method in pandas to get unique values from column pandas.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 19,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!