如何在 Pandas 中使用 nunique() 計算組內的唯一值?

Susan Sarandon
發布: 2024-10-18 15:44:03
原創
535 人瀏覽過

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>
登入後複製

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.

以上是如何在 Pandas 中使用 nunique() 計算組內的唯一值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板