【问题标题】:How to correctly sort a multi-indexed pandas DataFrame如何正确排序多索引熊猫 DataFrame
【发布时间】:2017-04-12 03:17:54
【问题描述】:

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

提前致谢

【问题讨论】:

  • 你可以试试df.sort_index(level=[0,1])

标签: python sorting pandas multi-index


【解决方案1】:

我觉得你需要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

【讨论】:

  • 您好,jezrael,感谢您的回复。这也是我的方法,但对我来说,我在 0 级得到了拆分。所以前三个重复在 df 的顶部,后三个在中间
  • 如何只对二级和三级进行排序? df = df.sort_index(level=[1,2])
  • 我刚刚意识到我的问题在于没有inplace=True,所以我只是返回了旧框架。现在正在工作。感谢您的帮助。
  • 是的,或者分配或inplace 是必要的。
  • 很高兴能帮到你!
猜你喜欢
  • 1970-01-01
  • 2018-07-04
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 2019-01-19
  • 1970-01-01
  • 2020-03-29
相关资源
最近更新 更多