After pivoting a dataframe with two values like below:
import pandas as pd
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'bar'],
'B' : ['one', 'one', 'two', 'two',
'two', 'two', 'one', 'two'],
'C' : [56, 2, 3, 4, 5, 6, 0, 2],
'D' : [51, 2, 3, 4, 5, 6, 0, 2]})
pd.pivot_table(df, values=['C','D'],rows='B',cols='A').unstack().reset_index()
When I unstack the pivot and reset the index two new columns 'level_0' and 0 are created. Level_0 contains the column names C and D and 0 contains the values.
level_0 A B 0
0 C bar one 2.0
1 C bar two 4.0
2 C foo one 28.0
3 C foo two 4.0
4 D bar one 2.0
5 D bar two 4.0
6 D foo one 25.5
7 D foo two 4.0
Is it possible to unstack the frame so each value (C,D) appears in a separate column or do I have to split and concatenate the frame to achieve this? Thanks.
edited to show desired output:
A B C D
0 bar one 2 2
1 bar two 4 4
2 foo one 28 25.5
3 foo two 4 4