富国沪深
set_index和unstack:df.set_index(['instance', 'algo']).unstack().swaplevels(1, 0, axis=1) profit time algo A B A Binstance x 10 39 0.5 0.9y 20 12 0.1 1.2z 13 14 0.7 0.6(df.set_index(['instance', 'algo']) .unstack() .swaplevel(1, 0, axis=1) .sort_index(axis=1))algo A B profit time profit timeinstance x 10 0.5 39 0.9y 20 0.1 12 1.2z 13 0.7 14 0.6另一种选择是使用pivotand swaplevel:(df.pivot('instance', 'algo', ['profit', 'time']) .swaplevel(1, 0, axis=1) .sort_index(axis=1))algo A B profit time profit timeinstance x 10.0 0.5 39.0 0.9y 20.0 0.1 12.0 1.2z 13.0 0.7 14.0 0.6