python pandas multi index如何向下移动行?

问题描述

嘿,我正在尝试使数据框准备好导出到乳胶中,但我不知道如何手动更改数据框的顺序,我可以对其进行排序等,但是我无法使其显示特定的我需要的订单:

    Hyper-parameter                  H-parameter values                             
CNN activation function          relu                         0.018254  0.018359
                                 tanh                         0.018249  0.018348
CNN filter layers                16                                NaN  0.018353
                                 32                                NaN  0.018338
                                 64                                NaN  0.018368
Feature collections              daytime                      0.017845  0.017947
                                 daytime + sensors            0.020593  0.020344
                                 daytime + wind + irradiance  0.017080  0.017311
                                 daytime + wind + sensors     0.019660  0.019819
                                 irradiance                   0.017133  0.017413
                                 pressure and humidity        0.017735  0.017828
                                 rain                         0.017983  0.017953
                                 sensors                      0.020103  0.020075
                                 temperature                  0.018081  0.017964
                                 univariate                   0.017827  0.017747
                                 wind                         0.017216  0.017571
                                 wind + irradiance            0.016605  0.017027
                                 wind + sensors               0.019407  0.019594
LSTM activation function         relu                         0.018299       NaN
                                 tanh                         0.018204       NaN
LSTM history vector length       60                           0.018246       NaN
                                 80                           0.018253       NaN
                                 100                          0.018255       NaN
Learning rate                    0.0001                       0.018424  0.018496
                                 0.0005                       0.018156  0.018259
                                 0.001                        0.018174  0.018305
MaxPooling layer size            3                            0.018212  0.018300
                                 5                            0.018230  0.018378
                                 7                            0.018313  0.018381
Second CNN kernel                False                             NaN  0.018407
                                 True                              NaN  0.018300
Second set of cnn+pooling layers False                             NaN  0.018333
                                 True                              NaN  0.018374

这是我的数据框,我真的很想控制Hyper参数的顺序。例如,我想将CNN activation functionCNN filter layers下移到Feature collections以下。关于如何完成此操作的任何想法都很棒。

供参考,下面是一个玩具示例:

df = pd.DataFrame({"a": [1,2,3,4,5,6]},index=['A','b','C','d','e','f'])
arrays = [[1,1,3],['red','blue','red','blue']]
df.index = pd.MultiIndex.from_arrays(arrays,names=('number','color')
df

              a
number color   
1      red    1
       blue   2
2      red    3
       blue   4
3      red    5
       blue   6

如何将“数字” 2的内容移到顶部?

解决方法

没关系,经过一个多小时的搜索,我发表了这篇文章,十分钟后,我找到了答案。使用问题底部的示例:

df.reindex([2,1,3],level=0)
df

              a
number color   
2      red    3
       blue   4
1      red    1
       blue   2
3      red    5
       blue   6