Find Unique Values in a pandas Column


How do you find unique values in a pandas DataFrame? You can use the unique() function.

Let’s look at an example. Here is some Python code that you can copy and execute in a Jupyter notebook.

# Let's create a DataFrame manually
# start with a dictionary of equal-length lists.
# The data has been modified from Wes McKinney's data 
data = {"state": ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada', "Arazona"],
        "year": [2000, 2001, 2002, 2001, 2002, 2003, 2001],
        "pop": [1.5, 1.7, 3.6, 2.4, 2.9, 3.2, 1.1]}
df = pd.DataFrame(data)

Get the unique values under the state column with the unique() method.

df['state'].unique()
array(['Ohio', 'Nevada', 'Arazona'], dtype=object)

How do you get the unique number of values in a column? To count the number of unique values in a specific column in a Pandas DataFrame you can use the unique() method.

Here is another way.

# this is another way
set(df['state'])

It returns this. This is a set. According to the website w3schools.com, “Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is unordered, unchangeable, and unindexed.”

{'Arazona', 'Nevada', 'Ohio'}

Leave a Reply