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

标签 python sorting pandas multi-index

我有一个多索引的 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

(请注意,我只发布了一个 antibodyantibody 索引下有许多类似的条目)但它们都具有相同的格式。尽管由于空间原因省略了中间的条目,但您可以看到我有 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

关于python - 如何正确排序多索引 pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843660/

相关文章:

r - 在维护行名称的 R 中对 data.frame 进行排序

c - 这个倒数计数算法的实现有什么问题呢?

python-3.x - 迭代数据帧的行,但将每一行保留为数据帧

python - 生物格式-Python 错误 : 'ascii' codec can't encode character u'\xb5' when using OMEXML()

Python 分类命令

linux - 使用 grep 查找两个大词表之间的差异

python - 如何仅将 pandas 系列中的整数相乘

python - Selenium -Python : unable to click an option in a select

python - 如何在 c 中读取传感器,然后在 python 中使用该输入

python - Pandas 中每一列的曲线拟合 + 外推值