Code python

Pandas DataFrame groupBy and then COUNT DISTINCT

Kontext Kontext visibility 21 comment 0 access_time 2 months ago language English

descriptionCode description

This code snippet shows you how to group a pandas DataFrame  via certain column and then do a distinct count of unique values in another column.

It is similar as COUNT DISTINCT aggregation functions in SQL. It also sort the values by the group by column.


Example output:

   category  value  user  group-count
0         A      0     5            7
80        A     80     4            7
70        A     70     7            7
60        A     60    10            7
50        A     50     9            7
..      ...    ...   ...          ...
29        J     29     9            7
19        J     19     9            7
9         J      9     9            7
89        J     89     8            7
99        J     99     7            7

[100 rows x 4 columns]
fork_rightFork
more_vert
copyright This page is subject to Site terms.
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts