I have this DataFrame :
Age Hgt Wgt
x y x y x y
0 26 24 160 164 95 71
1 35 37 182 163 110 68
2 57 52 175 167 89 65
It is a MultiIndex DataFrame.
I'm using pandas to get this final result:
x_new y_new parameter
0 26 24 Age
1 35 37 Age
2 57 52 Age
3 160 164 Hgt
4 182 163 Hgt
5 175 167 Hgt
6 95 71 Wgt
7 110 68 Wgt
8 89 65 Wgt
Basically, all the x columns are merged/stacked under one new column x_new, as well as y columns under y_new column. Always the x value should take the y value of the same raw and column.
This is what I tried to do:
First, I used melt() after I joined the column indices and became single index '_'.join(col).strip()
It created extra wrong rows. These wrong rows have wrong values, for example: Age_x and Hgt_y in the same row.
Remember, always, for example: Age_x and Age_y in the same row. Or, Hgt_x and Hgt_y are in the same row.
Second, I used stack(), and it gave me this result:
df.stack().reset_index(level=0, drop=True).reset_index()
index Age Hgt Wgt
0 x 26 160 95
1 y 24 164 71
2 x 35 182 110
3 y 37 163 68
4 x 57 175 89
5 y 52 167 65
I don't know what else I can do.
Is there a way to turn the MultiIndex DataFrame to the final result that I'm looking for using simple pandascode?