0

I want to create a dataframe that takes the 1st index and makes it into a column.

My grouping code: candy_df.groupby(['BAG', 'LOLLIPOP']).agg('count')['STICKID']

Right now my grouping returns this:

BAG      LOLLIPOP
011111     CHOCO          69
           VANILL         33
011112     CHOCO         133
           VANILL        129

I'd like to take the 1st index, LOLLIPOP, and make the different flavors be the columns:

BAG        CHOCO         VANILL
011111     69             33
011112     133            129
Christina Zhou
  • 1,483
  • 1
  • 7
  • 17

1 Answers1

0

candy_df.groupby(['BAG', 'LOLLIPOP'])['STICKID'].count().unstack()

This question was also answered under Question 4 under How to pivot a dataframe

Christina Zhou
  • 1,483
  • 1
  • 7
  • 17