我有一个数据框架:
df = pd.DataFrame.from_dict({'Close': {1: 14.03, 3: 14.02, 0: 79.88, 2: 80.31},
'High': {1: 14.3, 3: 14.33, 0: 80.22, 2: 81.19},
'Low': {1: 14.03, 3: 13.99, 0: 79.39, 2: 80.25},
'Open': {1: 14.18, 3: 14.25, 0: 79.79, 2: 80.97},
'Volume': {1: 1656782.0, 3: 2249159.0, 0: 1416232.0, 2: 1622784.0},
'Value': {1: 23244651.46, 3: 31533209.18, 0: 113128612.16, 2: 130325783.04},
'Code': {1: 'A2M', 3: 'A2M', 0: 'CBA', 2: 'CBA'}})
df.index = [0,1,0,1]
Close High Low Open Volume Value Code
0 14.03 14.30 14.03 14.18 1656782.0 2.324465e+07 A2M
1 14.02 14.33 13.99 14.25 2249159.0 3.153321e+07 A2M
0 79.88 80.22 79.39 79.79 1416232.0 1.131286e+08 CBA
1 80.31 81.19 80.25 80.97 1622784.0 1.303258e+08 CBA
如何将Code列的值作为第一级,当前列作为第二级,将列更改为多级索引?
CBA A2M
Close High Low Open Volume Value Close High Low Open Volume Value
0 79.88 80.22 79.39 79.79 1416232.0 1.131286e+08 14.02 14.33 13.99 14.25 2249159.0 31533209.18
1 80.31 81.19 80.25 80.97 1622784.0 1.303258e+08 14.03 14.30 14.03 14.18 1656782.0 23244651.46
谢谢。