简体   繁体   English

按一级MultiIndex对熊猫DataFrame进行排序

[英]Sorting a pandas DataFrame by one level of a MultiIndex

I have a MultiIndexed pandas DataFrame that needs sorting by one of the indexers. 我有一个MultiIndexed pandas DataFrame,需要按索引器之一进行排序。 Here is a snippet of the data: 以下是数据片段:

gene                      VIM  
treatment dose time            
TGFb      0.1  2    -0.158406  
          1    2     0.039158  
          10   2    -0.052608  
          0.1  24    0.157153  
          1    24    0.206030  
          10   24    0.132580  
          0.1  48   -0.144209  
          1    48   -0.093910  
          10   48   -0.166819  
          0.1  6     0.097548  
          1    6     0.026664  
          10   6    -0.008032  

I'm looking to sort the data so that the time index is in ascending order. 我正在寻找对数据进行排序,以便时间索引按升序排列。 My first thoughts was to use pandas.sort_values but it seems this doesn't work on the index. 我最初的想法是使用pandas.sort_values但似乎对索引不起作用。 Does anybody know of a way to do this? 有人知道这样做的方法吗? Thanks 谢谢

Use sort_index specifying level : 使用sort_index指定level

df.sort_index(level=2)

Or 要么

df.sort_index(level=-1)

Or 要么

df.sort_index(level='time')

All yield: 所有产量:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用“键”按 MultiIndex 的一级对 pandas DataFrame 进行排序 - Sorting a pandas DataFrame by one level of a MultiIndex with a "key" 仅转置 Pandas MultiIndex 数据帧的一层 - Transpose only one level of a pandas MultiIndex dataFrame 根据0级索引自定义排序多索引Pandas DataFrame的1级索引 - Custom sorting of the level 1 index of a multiindex Pandas DataFrame according to the level 0 index Pandas MultiIndex DataFrame 排序 - Pandas MultiIndex DataFrame Sorting 附加到 Multiindex pandas DataFrame 中的级别 - append to level in Multiindex pandas DataFrame python pandas multiIndex Dataframe,如何基于iloc选择一个级别 - python pandas multiIndex Dataframe, how to select one level based on iloc 为什么在具有一级索引的 MultiIndex 列的 pandas DataFrame 中的行为不同? - Why in a pandas DataFrame with a MultiIndex columns of one level indexing behaves differently? Pandas Dataframe MultiIndex将多级索引中的一个转换为另一轴,同时将另一级保留在原始轴中 - Pandas Dataframe MultiIndex transform one level of the multiindex to another axis while keeping the other level in the original axis 按 0 级索引的最后一个值对 Pandas MultiIndex 进行排序 - Sorting Pandas MultiIndex by the last value of level 0 index 对熊猫多索引中每个级别1的值进行排序 - Sorting values for every level 1 in pandas multiindex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM