Pandas : How to Find Unique Values in a Column

We can find unique values of a column in Pandas DataFrame using the unique() function.

The unique() method filters out only unique values from a dataframe column. In this tutorial, we will learn how to use the unique() method to find unique values in Pandas DataFrame columns with examples.

In Pandas rename column of DataFrame can be done using  pandas.DataFrame.rename() method.

We have a simple DataFrame with the dictionary of lists, indicates the fruits, price and quantity as the column names.

# import pandas library
import pandas as pd

# create DataFrame
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'orange', 'mango'],
                   'price': ['40', '80', '30', '40', '30', '80'],
                   'quantity': ['200', '300', '300', '400', '200', '800']
                   })

print(df)

Output

   fruits price quantity
0  orange    40      200
1   mango    80      300
2   apple    30      300
3  grapes    40      400
4  orange    30      200
5   mango    80      800

Find unique values of a single column in Pandas DataFrame

Let’s say if we need to find the unique values of a fruits column, we can use the unique() method as shown in the below code.

# import pandas library
import pandas as pd

# create DataFrame
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'orange', 'mango'],
                   'price': ['40', '80', '30', '40', '30', '80'],
                   'quantity': ['200', '300', '300', '400', '200', '800']
                   })

# get the unique value of column fruits
print(df.fruits.unique())

Output

['orange' 'mango' 'apple' 'grapes']

Find unique values in all columns in Pandas DataFrame

If we need to find the unique values of all the columns in Pandas DataFrame, we need to iterate the columns using the for loop and then use the unique() method on each column name.

# import pandas library
import pandas as pd

# create pd DataFrame
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'orange', 'mango'],
                   'price': ['40', '80', '30', '40', '30', '80'],
                   'quantity': ['200', '300', '300', '400', '200', '800']
                   })

# get the unique value of all columns
for col in df:
  print(df			
.unique())

Output

['orange' 'mango' 'apple' 'grapes']
['40' '80' '30']
['200' '300' '400' '800']

Find and count unique values of a single column in Pandas DataFrame

We can even count the occurrence of unique values in a single column using the method value_counts() method.

# import pandas library
import pandas as pd

# create DataFrame
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'orange', 'mango'],
                   'price': ['40', '80', '30', '40', '30', '80'],
                   'quantity': ['200', '300', '300', '400', '200', '800']
                   })

# get the count unique values of column fruits
print(df.fruits.value_counts())

Output

orange    2
mango     2
apple     1
grapes    1
Name: fruits, dtype: int64
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Sort Dictionary By Value In Python

Sort Dictionary by value in Python

Table of Contents Hide Python sorted() function to rescueSyntax: sorted(iterable, key, reverse)Sort a Dictionary by ValueExample 1: Basic Sorting in PythonExample 2 : Sorting different Data Types in PythonExample 3: Sort…
View Post