How to Count Unique Values per Groups with Pandas?

Susan Sarandon
Release: 2024-10-18 15:49:03
Original
921 people have browsed it

How to Count Unique Values per Groups with Pandas?

Counting Unique Values per Groups with Pandas

When working with tabular data, it often becomes necessary to count the unique occurrences of values within specific groups. To achieve this in Python using the Pandas library, we can utilize the groupby() and nunique() methods.

Problem Explanation:

To illustrate the problem, consider the following dataset:

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 at hand is to count the unique ID values within each domain.

Solution:

To count unique values per group, we can use the following code:

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

The groupby() method groups the data by the domain column, while the nunique() method counts the unique occurrences of ID within each group. The output is a Series with the domain names as index and the corresponding unique counts as values.

domain
vk.com        3
twitter.com   2
facebook.com  1
google.com    1
Copy after login

Additional Notes:

  • If the domain column values contain single quotes ('), you can remove them before grouping using the str.strip("'") method.
  • To retain the column name in the output, use the agg() method with the pd.Series.nunique function.

Example with String Manipulation:

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

Example with agg():

<code class="python">df = df.groupby(by='domain', as_index=False).agg({'ID': pd.Series.nunique})</code>
Copy after login

The above is the detailed content of How to Count Unique Values per Groups with 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!