Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I groupby a DataFrame at the same time I count the values and put in different columns?

I have a DataFrame that looks like the one below

Index  Category  Class
 0        1        A
 1        1        A
 2        1        B
 3        2        A
 4        3        B
 5        3        B

And I would like to get an output data frame that groups by category and have one column for each of the classes with the counting of the occurrences of that class in each category, such as the one below

Index Category   A   B
 0      1        2   1
 1      2        1   0
 2      3        0   2

So far I’ve tried various combinations of the groupby and agg methods, but I still can’t get what I want. I’ve also tried df.pivot_table(index='Category', columns='Class', aggfunc='count'), but that return a DataFrame without columns. Any ideas of what could work in this case?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use aggfunc="size" to achieve your desired result:

>>> df.pivot_table(index='Category', columns='Class', aggfunc='size', fill_value=0)

Class     A  B
Category
1         2  1
2         1  0
3         0  2

Alternatively, you can use .groupby(...).size() to get the counts, and then unstack to reshape your data as well:

>>> df.groupby(["Category", "Class"]).size().unstack(fill_value=0)

Class     A  B
Category
1         2  1
2         1  0
3         0  2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading