Get unique values from a column in Pandas DataFrame

Let’s discuss how to get unique values from a column in Pandas DataFrame.

Create a simple dataframe with dictionary of lists, say columns name are A, B, C, D, E with duplicate elements.

Now, let’s get the unique values of a column in this dataframe.

Example #1: Get the unique values of ‘B’ column

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd
  
# create a dictionary with five fields each
data = {
    'A':['A1', 'A2', 'A3', 'A4', 'A5'], 
    'B':['B1', 'B2', 'B3', 'B4', 'B4'], 
    'C':['C1', 'C2', 'C3', 'C3', 'C3'], 
    'D':['D1', 'D2', 'D2', 'D2', 'D2'], 
    'E':['E1', 'E1', 'E1', 'E1', 'E1'] }
  
# Convert the dictionary into DataFrame 
df = pd.DataFrame(data)
  
# Get the unique values of 'B' column
df.B.unique()
chevron_right

Output:



 
Example #2: Get the unique values of ‘E’ column

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd
  
# create a dictionary with five fields each
data = {
    'A':['A1', 'A2', 'A3', 'A4', 'A5'], 
    'B':['B1', 'B2', 'B3', 'B4', 'B4'], 
    'C':['C1', 'C2', 'C3', 'C3', 'C3'], 
    'D':['D1', 'D2', 'D2', 'D2', 'D2'], 
    'E':['E1', 'E1', 'E1', 'E1', 'E1'] }
  
# Convert the dictionary into DataFrame 
df = pd.DataFrame(data)
  
# Get the unique values of 'E' column
df.E.unique()
chevron_right

Output:

Example #3: Get number of unique values in a column

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd
  
# create a dictionary with five fields each
data = {
    'A':['A1', 'A2', 'A3', 'A4', 'A5'], 
    'B':['B1', 'B2', 'B3', 'B4', 'B4'], 
    'C':['C1', 'C2', 'C3', 'C3', 'C3'], 
    'D':['D1', 'D2', 'D2', 'D2', 'D2'], 
    'E':['E1', 'E1', 'E1', 'E1', 'E1'] }
  
# Convert the dictionary into DataFrame 
df = pd.DataFrame(data)
  
# Get number of unique values in column 'C'
df.C.nunique(dropna = True)
chevron_right

Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




Article Tags :