In this tutorial, we will look at how to get a list of unique values in a pandas dataframe column. Additionally, we will also look at how to get a count of each unique value within the column and the total unique count.
First, let’s create a sample dataframe that we will be using throughout this tutorial for demonstrating the usage.
import pandas as pd # dataframe of height and weight football players df = pd.DataFrame({ 'Height': [167, 175, 170, 186, 190, 188, 158, 169, 183, 180], 'Weight': [65, 70, 72, 80, 86, 94, 50, 58, 78, 85], 'Team': ['A', 'A', 'B', 'B', 'B', 'C', 'A', 'C', 'B', 'C'] }) # display the dataframe print(df)
Output:
Height Weight Team 0 167 65 A 1 175 70 A 2 170 72 B 3 186 80 B 4 190 86 B 5 188 94 C 6 158 50 A 7 169 58 C 8 183 78 B 9 180 85 C
Now we have a dataframe with 10 rows and three columns storing information on height, weight, and teams of top scorers in a football competition.
1. List of all unique values in a pandas dataframe column
You can use the pandas unique()
function to get the different unique values present in a column. It returns a numpy array of the unique values in the column. For example, let’s see what are the unique values present in the column “Team” of the dataframe “df” created above.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
# unique values in column "Team" print(df['Team'].unique()) # check the return type print(type(df['Team'].unique()))
Output:
['A' 'B' 'C'] <class 'numpy.ndarray'>
You can see we get a numpy array of all the unique values present in the column “Team” – “A”, “B”, and “C”.
For more on the pandas unique() function, refer to its documentation.
2. Count of unique values in a column
If you just need the count of unique values present in a pandas dataframe column, you can use the pandas nunique()
function. It returns the number of unique values present in the dataframe as an integer. For example, let’s count the number of unique values in the column “Team” of the dataframe “df”.
# count of unique values print(df['Team'].nunique())
Output:
3
We get 3
as the output because there are three unique values present in the column “Team”. Note that you can use the nunique() function to count the unique values in a row as well.
3. Count of each unique value in a column
You can use the pandas value_counts()
function to get the number of times each unique value occurs in a column. For example, let’s find the what’s the count of each unique value in the “Team” column.
# value counts of each unique value print(df['Team'].value_counts())
Output:
B 4 A 3 C 3 Name: Team, dtype: int64
You can see that the value “B” occurs 4 times, and “A” and “C” occur 3 times each in the column “Team”. The value_counts() is a nice function to check the distribution of data points across a categorical field.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on common column operations in pandas –