Home > database >  How to group data by more than one column with groupby - if possible
How to group data by more than one column with groupby - if possible

Time:03-11

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:

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.

CodePudding user response:

Use a list:

(df.groupby(['GradeEntering', 'Race', 'Liveswith'])['Race']
   .value_counts(normalize=False)
 )
  • Related