How can I convert the following pandas dataframe:
dw1 dw2
db_c c_dat a e
c_idx b f
db_s s_dat c g
s_idx d h
to this:
db_c c_dat dw1 a
c_dat dw2 e
c_idx dw1 b
c_idx dw2 f
db_s s_dat dw1 c
s_dat dw2 g
s_idx dw1 d
s_idx dw2 h
The count of the columns in the first dataframe is variable.
You need to stack
as Series, the columns with automatically form a new index level:
out = df.stack()
Output:
db_c c_dat dw1 a
dw2 e
c_idx dw1 b
dw2 f
db_s s_dat dw1 c
dw2 g
s_idx dw1 d
dw2 h
dtype: object