如何正确排序多索引 pandas DataFrame

How to correctly sort a multi-indexed pandas DataFrame

我有一个多索引 pandas 数据框,如下所示:

Antibody                 Time Repeats           
Akt                      0    1         1.988053
                              2         1.855905
                              3         1.416557
                         5    1         1.143599
                              2         1.151358
                              3         1.272172
                         10   1         1.765615
                              2         1.779330
                              3         1.752246
                         20   1         1.685807
                              2         1.688354
                              3         1.614013
                         .....        ....
                         0    4         2.111466
                              5         1.933589
                              6         1.336527
                         5    4         2.006936
                              5         2.040884
                              6         1.430818
                         10   4         1.398334
                              5         1.594028
                              6         1.684037
                         20   4         1.529750
                              5         1.721385
                              6         1.608393

(注意我只发了一个antibody,在antibody索引下有很多类似的条目)但是它们都具有相同的格式。尽管为了 space 而遗漏了中间的条目,您可以看到我有 6 个实验重复,但它们没有正确组织。我的问题是:如何让 DataFrame 聚合所有重复项。所以输出看起来像这样:

Antibody                 Time Repeats           
Akt                      0    1         1.988053
                              2         1.855905
                              3         1.416557
                              4         2.111466
                              5         1.933589
                              6         1.336527
                         5    1         1.143599
                              2         1.151358
                              3         1.272172
                              4         2.006936
                              5         2.040884
                              6         1.430818
                         10   1         1.765615
                              2         1.779330
                              3         1.752246
                              4         1.398334
                              5         1.594028
                              6         1.684037
                         20   1         1.685807
                              2         1.688354
                              3         1.614013
                              4         1.529750
                              5         1.721385
                              6         1.60839
                         .....        ....

提前致谢

我认为你需要sort_index:

df = df.sort_index(level=[0,1,2])
print (df)
Antibody  Time  Repeats
Akt       0     1          1.988053
                2          1.855905
                3          1.416557
                4          2.111466
                5          1.933589
                6          1.336527
          5     1          1.143599
                2          1.151358
                3          1.272172
                4          2.006936
                5          2.040884
                6          1.430818
          10    1          1.765615
                2          1.779330
                3          1.752246
                4          1.398334
                5          1.594028
                6          1.684037
          20    1          1.685807
                2          1.688354
                3          1.614013
                4          1.529750
                5          1.721385
                6          1.608393
Name: col, dtype: float64

或者您可以省略参数 levels:

df = df.sort_index()
print (df)
Antibody  Time  Repeats
Akt       0     1          1.988053
                2          1.855905
                3          1.416557
                4          2.111466
                5          1.933589
                6          1.336527
          5     1          1.143599
                2          1.151358
                3          1.272172
                4          2.006936
                5          2.040884
                6          1.430818
          10    1          1.765615
                2          1.779330
                3          1.752246
                4          1.398334
                5          1.594028
                6          1.684037
          20    1          1.685807
                2          1.688354
                3          1.614013
                4          1.529750
                5          1.721385
                6          1.608393
Name: col, dtype: float64