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

pandas groupby at row and column level

My dataframe is in the below format:

col1 col2 col3
A1    B1    t1
A2    B2    t2
A1    B1    t1
A1    B2    t2

I am grouping a dataframe as below:

df.groupby(['col1', 'col2'])['col3'].count()

which gives me the stats as:

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

A1 B1 2
A1 B2 1
A2 B2 1

What I would like the count be split on the basis of col3 like:

      t1   t2 
A1 B1 1    1
A1 B2 0    1
A2 B2 0    1

How could I achieve something like this?

>Solution :

You can use a pivot_table:

out = (df.assign(count=1)
         .pivot_table(index=['col1', 'col2'], columns='col3', values='count',
                      aggfunc='count', fill_value=0)
      )

output:

col3       t1  t2
col1 col2        
A1   B1     2   0
     B2     0   1
A2   B2     0   1
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