Home > Backend Development > Python Tutorial > How to Count Unique Values Within Groups Using nunique() in Pandas?

How to Count Unique Values Within Groups Using nunique() in Pandas?

Susan Sarandon
Release: 2024-10-18 15:44:03
Original
568 people have browsed it

How to Count Unique Values Within Groups Using nunique() in Pandas?

Counting Unique Values per Group in Pandas with nunique

In pandas, counting unique values in a group is possible using the nunique() method. This is particularly useful when working with data where you need to determine the number of distinct values within specific categories or groups.

Problem:

Consider a DataFrame with the following data:

ID domain
123 vk.com
123 vk.com
123 twitter.com
456 vk.com
456 facebook.com
456 vk.com
456 google.com
789 twitter.com
789 vk.com

The task is to count the unique IDs for each domain in this DataFrame.

Solution:

To count unique values per group, use the nunique() method with the desired grouping columns. In this case, the domain column represents the groups:

<code class="python">df = df.groupby('domain')['ID'].nunique()
print(df)</code>
Copy after login

Output:

domain count
facebook.com 1
google.com 1
twitter.com 2
vk.com 3

Additional Considerations:

  • To remove any single quotes (') from the domain values, use the str.strip("'") method before grouping.
  • To preserve the column name, use the as_index=False argument in groupby() and pass the nunique() method to the agg() function.

The above is the detailed content of How to Count Unique Values Within Groups Using nunique() in Pandas?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template