Pandas 具有多索引列的数据框 - 更改级别

Pandas dataframe with multiindex column - change levels

源数据:

pd.pivot_table(ceshi, values=['num1', 'num2'], index=['date'],
            columns=['c'], aggfunc={'num1': np.sum,'num2': np.sum}, fill_value=0)

如何转化为:

使用DataFrame.swaplevel with DataFrame.sort_index:

df = pd.pivot_table(ceshi, values=['num1', 'num2'], index=['date'],
                   columns=['c'], aggfunc={'num1': np.sum,'num2': np.sum}, fill_value=0)

然后:

df = df.swaplevel(1,0, axis=1).sort_index(axis=1)