I have a dataframe with a column labels that I want to move into a data column, reshaping and ignoring index values.
import pandas as pd
before = pd.DataFrame.from_dict({0:['A',1,2,3],
1: ['B',4,5,6],
2: ['C',7,8,9],
3: ['D',10,11,12]},
columns=['C0', 'C1', 'C2', 'C3'],
orient='index')
before
C0 C1 C2 C3
0 A 1 2 3
1 B 4 5 6
2 C 7 8 9
3 D 10 11 12
after
0 1 2
0 A C1 1
1 A C2 2
2 A C3 3
3 B C1 4
4 B C2 5
5 B C3 6
6 C C1 7
7 C C2 8
8 C C3 9
9 D C1 10
10 D C2 11
11 D C3 12
I've tried to df.pivot()
without success as the column labels don't figure into df.pivot()
only row and column data.
Is there some way I can achieve this without loops?