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 to group data by more than one column with groupby – if possible

Using groupby with pandas, I can get a count and percentage from a spreadsheet that will tell me the racial breakdown of our school by "Grade" OR the "Livewith" (Single Parent) breakdown.

df.groupby('GradeEntering)['Race'].value_counts(normalize = False).

What I can’t find out how to do is get it broken down by "GradeEntering" AND "RACE" AND "Liveswith"

I have a spreadsheet with hundreds of rows like below:

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

GradeEntering Race Liveswith
Kindergarten W Both Parents
Kindergarten B Mother
Kindergarten W Father
First W Both Parents
Kindergarten W Both Parents
Kindergarten B Mother
Kindergarten W Father
First W Both Parents

Desired output would be Number of "Kindergarten" who are "W" and live with "Both Parents", then "Kindergarten" who are "W" and live with "Mother", and so on. So I have both counts and percentages. This is important to us as we try to represent our community population within our small charter school.

>Solution :

Use a list:

(df.groupby(['GradeEntering', 'Race', 'Liveswith'])['Race']
   .value_counts(normalize=False)
 )
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