15.1 C
London
Friday, July 5, 2024
HomePandas in PythonGeneral Functions in PythonHow to Count Unique Values in Pandas (With Examples)

How to Count Unique Values in Pandas (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the nunique() function to count the number of unique values in a pandas DataFrame.

This function uses the following basic syntax:

#count unique values in each column
df.nunique()

#count unique values in each row
df.nunique(axis=1)

The following examples show how to use this function in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'points': [8, 8, 13, 13, 22, 22, 25, 29],
                   'assists': [5, 8, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 11, 6, 6, 5, 9, 12]})

#view DataFrame
df

	team	points	assists	rebounds
0	A	8	5	11
1	A	8	8	8
2	A	13	7	11
3	A	13	9	6
4	B	22	12	6
5	B	22	9	5
6	B	25	9	9
7	B	29	4	12

Example 1: Count Unique Values in Each Column

The following code shows how to count the number of unique values in each column of a DataFrame:

#count unique values in each column
df.nunique()

team        2
points      5
assists     5
rebounds    6
dtype: int64

From the output we can see:

  • The ‘team’ column has 2 unique values
  • The ‘points’ column has 5 unique values
  • The ‘assists’ column has 5 unique values
  • The ‘rebounds’ column has 6 unique values

Example 2: Count Unique Values in Each Row

The following code shows how to count the number of unique values in each row of a DataFrame:

#count unique values in each row
df.nunique(axis=1)

0    4
1    2
2    4
3    4
4    4
5    4
6    3
7    4
dtype: int64

From the output we can see:

  • The first row has 4 unique values
  • The second row has 2 unique values
  • The third row has 4 unique values

And so on.

Example 3: Count Unique Values by Group

The following code shows how to count the number of unique values by group in a DataFrame:

#count unique 'points' values, grouped by team
df.groupby('team')['points'].nunique()

team
A    2
B    3
Name: points, dtype: int64

From the output we can see:

  • Team ‘A’ has 2 unique ‘points’ values
  • Team ‘B’ has 3 unique ‘points’ values

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Count Observations by Group in Pandas
How to Count Missing Values in Pandas
How to Use Pandas value_counts() Function

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories