In the following the values _0,_1
have been used where there should be
spaces, I wasn't able to create the example with spaces here as pandas didn't
allow them. They are present in the excel file that I'm reading from though.
1,2,3
are not values which can be relied on in any solution, they're just
filling for this example.
What I would like to do is convert an additional heading into a column, so that there is only one heading for the data.
Some example data:
ef = pd.DataFrame({
'_0' : ['loc', 1, 2, 3],
'a' : ['x', 1, 2, 3],
'_1' : ['y', 1, 2, 3],
'_2' : ['z', 1, 2, 3],
'b' : ['x', 1, 2, 3],
'_3' : ['y', 1, 2, 3],
'_4' : ['z', 1, 2, 3],
'c' : ['x', 1, 2, 3],
'_5' : ['y', 1, 2, 3],
'_6' : ['z', 1, 2, 3],
})
Which outputs
In [98]: ef
Out[98]:
_0 a _1 _2 b _3 _4 c _5 _6
0 loc x y z x y z x y z
1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3
Without the underscores this is
a b c
0 loc x y z x y z x y z
1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3
And I would like to get it into the form
loc type x y z
1 a 1 1 1
1 b 1 1 1
1 c 1 1 1
2 a 2 2 2
2 b 2 2 2
2 c 2 2 2
3 a 3 3 3
3 b 3 3 3
3 c 3 3 3
How can this be done using pandas?