通过单个列搜索具有分层索引的熊猫数据框

问题描述 投票:1回答:2

我正在使用此数据框:

import pandas as pd
df = pd.DataFrame([['A', 'one',  105], ['A', 'two',  101], ['A', 'three',  103],
                      ['B','one',  101], ['B','two',  1102], ['B','three',  1050]],
                   columns=['c1', 'c2', 'c3'])
df = df.set_index(['c1', 'c2'])
df

哪个返回

               c3
c1    c2    
A     one     105
      two     101
      three   103
B     one     101
      two     1102
      three   1050

...我想按列c3排序,保留行和c1排序,以得到此信息:

              c3
c1    c2    
A     one     105
      three   103
      two     101
B     two     1102
      three   1050
      one     101

我无法提出一种不会混淆c1排序的方法。特别是,最后一个df.sort_index()返回KeyError: 'c1'

pandas search indexing hierarchical
2个回答
2
投票

您可以做的IIUC:

out = (df.sort_values(['c3','c1'],ascending=False)
      .reindex(df.index.get_level_values(0).unique(),level=0))

            c3
c1 c2         
A  one     105
   three   103
   two     101
B  two    1102
   three  1050
   one     101

0
投票

我想您可以使用:

df.sort_values(['c1','c3'], ascending=False).groupby(['c1','c3']).agg(lambda x: x)

输出:

            c3
c1 c2         
B  two    1102
   three  1050
   one     101
A  one     105
   three   103
   two     101
© www.soinside.com 2019 - 2024. All rights reserved.