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:
위 내용은 Pandas에서 nunique()를 사용하여 그룹 내의 고유 값을 계산하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!