Table of Contents
The process of counting the unique combinations of two columns in a Pandas dataframe involves identifying all distinct combinations of values from the two columns and determining the frequency of each unique combination. This can be achieved by using the Pandas library in Python, specifically the groupby and nunique functions, which allow for the grouping and counting of distinct values in a dataframe. By utilizing these functions, one can efficiently calculate the total number of unique combinations present in the two columns of the dataframe.
Pandas: Count Unique Combinations of Two Columns
You can use the following syntax to count the number of unique combinations across two columns in a pandas DataFrame:
df[['col1', 'col2']].value_counts().reset_index(name='count')
The following example shows how to use this syntax in practice.
Example: Count Unique Combinations of Two Columns in Pandas
Suppose we have the following pandas DataFrame that shows the team and position of various basketball players:
import pandas as pd #create dataFrame df = pd.DataFrame({'team': ['Mavs', 'Mavs', 'Mavs', 'Mavs', 'Heat', 'Heat', 'Heat', 'Heat'], 'position': ['Guard', 'Guard', 'Guard', 'Forward', 'Guard', 'Forward', 'Forward', 'Guard']}) #view DataFrame df team position 0 Mavs Guard 1 Mavs Guard 2 Mavs Guard 3 Mavs Forward 4 Heat Guard 5 Heat Forward 6 Heat Forward 7 Heat Guard
We can use the following syntax to count the number of unique combinations of team and position:
df[['team', 'position']].value_counts().reset_index(name='count') team position count 0 Mavs Guard 3 1 Heat Forward 2 2 Heat Guard 2 3 Mavs Forward 1
From the output we can see:
- There are 3 occurrences of the Mavs-Guard combination.
- There are 2 occurrences of the Heat-Forward combination.
- There are 2 occurrences of the Heat-Guard combination.
- There is 1 occurrence of the Mavs-Forward combination.
Note that you can also sort the results in order of count ascending or descending.
For example, we can use the following code to sort the results in order of count ascending:
df[['team', 'position']].value_counts(ascending=True).reset_index(name='count') team position count 0 Mavs Forward 1 1 Heat Forward 2 2 Heat Guard 2 3 Mavs Guard 3
The results are now sorted by count from smallest to largest.
Note: You can find the complete documentation for the pandas value_counts() function .
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I count the unique combinations of two columns in a Pandas dataframe?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-count-the-unique-combinations-of-two-columns-in-a-pandas-dataframe/
stats writer. "How can I count the unique combinations of two columns in a Pandas dataframe?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-count-the-unique-combinations-of-two-columns-in-a-pandas-dataframe/.
stats writer. "How can I count the unique combinations of two columns in a Pandas dataframe?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-count-the-unique-combinations-of-two-columns-in-a-pandas-dataframe/.
stats writer (2024) 'How can I count the unique combinations of two columns in a Pandas dataframe?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-count-the-unique-combinations-of-two-columns-in-a-pandas-dataframe/.
[1] stats writer, "How can I count the unique combinations of two columns in a Pandas dataframe?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I count the unique combinations of two columns in a Pandas dataframe?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
