Skip to main content
 首页 » 编程设计

python之如何正确排序多索引 pandas DataFrame

2024年10月17日10Terrylee

我有一个多索引的 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 索引下有许多类似的条目)但它们都具有相同的格式。尽管为了空间而漏掉了中间的条目,你可以看到我有 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