create a dataframe from the grouped columns of a grouped by one

I think you are looking for unstack:

out = (sales.groupby(['date_block_num','shop_id'])
           ['item_cnt_day'].sum()
           .unstack('date_block_num')
      )

Also you can do:

out = sales.pivot_table(index='shop_id', 
                        columns='dat_block_num',
                        values='item_cnt_day',
                        aggfunc='sum')

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top