Reputation: 2269
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
Reputation: 214957
You can call unstack
on results
directly with level=1
(vc):
results.unstack(level=1)
Or more explicitly:
results.unstack("vc")
Upvotes: 3