The unique() function removes all duplicate values on a column and returns a single value for multiple same values. In this article, we will discuss how we can get unique values from a column in Pandas DataFrame.
Creating a Pandas Dataframe with Duplicate Elements
Create a sample Pandas dataframe with a dictionary of lists, say columns names are A, B, C, D, and E with duplicate elements.
Python3
import pandas as pd
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' ]}
df = pd.DataFrame(data)
|

Get unique values from a column in Pandas DataFrame
Below are some examples by which we can get the unique values of a column in this dataframe.
- Get the Unique Values of ‘B’ Column
- Get the Unique Values of ‘E’ Column
- Get Number of Unique Values in a Column
- Using set() to Eliminate Duplicate Values from a Column
- Using pandas.concat() and Unique() Methods
- Using Series.drop_duplicates()
Get the Unique Values of ‘B’ Column
In this example, we are retrieving and printing the unique values from the ‘B’ column using the unique()
method. The resulting unique values are ['B1', 'B2', 'B3', 'B4']
.
Python3
import pandas as pd
df = pd.DataFrame(data)
df.B.unique()
|
Output
array(['B1', 'B2', 'B3', 'B4'], dtype=object)
Get the Unique Values of Pandas in ‘E’ Column
In this example, we create a pandas DataFrame from a dictionary and then retrieves the unique values from the ‘E’ column using the unique()
method. The resulting unique values are ['E1']
.
Python3
import pandas as pd
df = pd.DataFrame(data)
df.E.unique()
|
Output
array(['E1'], dtype=object)
Get Number of Unique Values in a Column
In this example, we create a pandas DataFrame from a dictionary and then calculates and prints the number of unique values in the ‘C’ column, excluding NaN values. The result is 3, indicating there are three unique values in column ‘C’.
Python3
import pandas as pd
df = pd.DataFrame(data)
df.C.nunique(dropna = True )
|
Output
3
Eliminate Duplicate Values from a Column using set()
In this example, we create a pandas DataFrame from a dictionary and then uses the set()
function to extract unique values from column ‘C’, eliminating duplicates. The resulting set, {'C1', 'C2', 'C3'}
, represents the unique values in column ‘C’.
Python3
import pandas as pd
df = pd.DataFrame(data)
unique_values_set = set (df[ 'C' ])
print (unique_values_set)
|
Output
{'C1', 'C2', 'C3'}
Using pandas.concat() and Unique() Methods
In this example, we create a pandas DataFrame from a dictionary and then concatenates unique values from all columns using pd.concat()
. The resulting NumPy array, when printed, displays all unique values from columns ‘A’ to ‘E’.
Python3
import pandas as pd
df = pd.DataFrame(data)
unique_values_all_columns = pd.concat([df[col].unique() for col in df.columns])
print (unique_values_all_columns)
|
Output
['A1' 'A2' 'A3' 'A4' 'A5' 'B1' 'B2' 'B3' 'B4' 'C1' 'C2' 'C3' 'D1' 'D2' 'E1']
Using Series.drop_duplicates()
In this example, we create a pandas DataFrame from a dictionary and removes duplicates from columns ‘A’ and ‘D’ using the drop_duplicates()
method. The resulting DataFrame, when printed, displays the unique values in columns ‘A’ and ‘D’, with NaN values where duplicates were removed from ‘D’.
Python3
import pandas as pd
df = pd.DataFrame(data)
df[ 'A' ] = df[ 'A' ].drop_duplicates()
df[ 'D' ] = df[ 'D' ].drop_duplicates()
print (df)
|
Output
A B C D E
0 A1 B1 C1 D1 E1
1 A2 B2 C2 D2 E1
2 A3 B3 C3 NaN E1
3 A4 B4 C3 NaN E1
4 A5 B4 C3 NaN E1
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Dec, 2023
Like Article
Save Article