0

I have this data frame: enter image description here

There is a column "CD" which has the city name, a column "FAC18" which is the number of people each line represents. And I have a column "S3P1" which is the academic level and is type int. When I group it by "CD" with hab_ciudad = cuestio.groupby('CD')["FAC18"].sum() I get:

enter image description here

Where I have summed over "FAC18". Now, I want also to group by academic level ("S3P1"). I want it to look like this:

enter image description here

where the columns are the values of "S3P1" and the sum is made over "FAC18". I tried this code: test = cuestio.groupby(['CD','S3P1'])["FAC18"].sum()

But I get this:

enter image description here

What's the syntax to get the form I want?

SAR_90
  • 71
  • 7

1 Answers1

0

After your groupby, you need to use pivot:

test = cuestio.groupby(['CD','S3P1'])["FAC18"].sum()
test = test.pivot(index='CD', columns='S3P1', values='FAC18')
nagini
  • 11
  • 2