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

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Isidentifier()

Python String isidentifier()

Table of Contents Hide isidentifier() Syntaxisidentifier() Parametersisidentifier() Return ValueExample 1: How isidentifier() works? Example 1: How to use isidentifier() method? The Python String isidentifier() method is a built-in function that…
View Post
Python List Extend()

Python List extend()

Table of Contents Hide Syntax of List extend() extend() ParametersReturn Value from List extend()Difference between append() and extend() method in PythonExample 1: How to use List extend() MethodExample 2: Add Elements…
View Post
Syntaxerror Unexpected Eof While Parsing

SyntaxError: unexpected EOF while parsing

Table of Contents Hide What is unexpected EOF while parsing error?Scenario 1 – Incomplete parameters may cause this kind of errors.Scenario 2: Incomplete functions along with statements, loops, try and…
View Post
Python String Swapcase()

Python String swapcase()

Table of Contents Hide swapcase() Syntaxswapcase() Parametersswapcase() Return ValueExample: Python Program to change the case of a given string Python string swapcase() method is a built-in function that converts all…
View Post
Graphviz'S Executables Not Found

Graphviz’s executables not found

Table of Contents Hide How to Fix InvocationException: graphviz’s executables not found?On WindowsOn LinuxOn macOS Graphviz is open source graph visualization software that helps represent structural information as diagrams of…
View Post