Comment compter les valeurs uniques par groupe avec Pandas ?

Susan Sarandon
Libérer: 2024-10-18 15:49:03
original
920 Les gens l'ont consulté

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>
Copier après la connexion

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
Copier après la connexion

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>
Copier après la connexion

Example with agg():

<code class="python">df = df.groupby(by='domain', as_index=False).agg({'ID': pd.Series.nunique})</code>
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!