I have a following dataframe
multicol = pd.MultiIndex.from_arrays([['1','1','2','2','2'],
['A','B','A','B','C']], names=('Num','Base'))
df = pd.DataFrame([[1,2,3,4,5],[2,1,2,3,4],[3,2,1,2,3],
[4,3,2,1,2],[5,4,3,2,1]], index=multicol, columns=multicol)
I want to reshape it to be like this
| Num | Base | Base | Value |
+-----+------+------+-------+
|(1,1)| A | A | 1 |
+-----+------+------+-------+
|(1,1)| A | B | 2 |
+-----+------+------+-------+
|(1,1)| B | A | 2 |
+-----+------+------+-------+
|(1,1)| B | B | 1 |
+-----+------+------+-------+
|(1,2)| A | A | 3 |
+-----+------+------+-------+
.....................
.....................
|(2,2)| C | C | 1 |
+-----+------+------+-------+
I have tried to use stack()
, but the new order is Num | Base | Base | Num | value |
. How to achieve a new dataframe with a result likes the one I expected?
Thank you.