Mark Ginsburg
Mark Ginsburg

Reputation: 2269

How to Pivot the result of an aggregated series

I have a series called 'results' I created with a

groupby

on 2 columns 'dt' and 'vc', summing a third numeric column 'numcol'.

The result looks like this:

dt     vc       
abc    ghi       3.2
       jkl      44.1
       mmm      15.2
xyz    def      11.3
       hhh       8.2
       jjj       4.4

Wanting to do a pivot, I create a dataframe:

resultsdf = results.to_frame() 

It looks like this :

                   numcol
dt    vc       
abc   ghi          3.2
      jkl         44.1
[....]

How to pivot resultsdf such that the index is dt, the columns are vc, and the values of the cells are numcol?

I had trouble resetting index of this structure and then setting to dt.

Upvotes: 1

Views: 33

Answers (1)

akuiper
akuiper

Reputation: 214957

You can call unstack on results directly with level=1(vc):

results.unstack(level=1)

enter image description here

Or more explicitly:

results.unstack("vc")

Upvotes: 3

Related Questions