在Pandas MultiIndex中获得特殊的组 [英] Get special group in pandas multiindex

查看:92
本文介绍了在Pandas MultiIndex中获得特殊的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有MultiIndex的DataFrame,如下所示:

I have a DataFrame with MultiIndex like this:

In [5]: df
Out[5]:
                 a   b
lvl0 lvl1 lvl2
A0   B0   C0     0   1
          C1     2   3
          C2     4   5
          C3     6   7
     B1   C0     8   9
          C1    10  11
          C2    12  13
          C3    14  15
A1   B0   C0    16  47
          C1    18  49
          C2    20  41
          C3    22  43
     B1   C0    24  25
          C1    26  27
          C2    28  29
          C3    30  31
A2   B0   C0    32  33
          C1    34  35
          C2    36  37
          C3    38  39
     B1   C0    40  41
          C1    42  43
          C2    44  45
          C3    46  47

我想在每个 lvl0 索引中获得特殊的 lvl1 组.在这种情况下,请选择 b 列具有最大值的组,结果可能像这样:

I want get the special lvl1 group in each lvl0 index. In this case, choice the group where column b has max value, result may like this:

                 a   b
lvl0 lvl1 lvl2
A0   B1   C0     8   9
          C1    10  11
          C2    12  13
          C3    14  15
A1   B0   C0    16  47
          C1    18  49
          C2    20  41
          C3    22  43
A2   B1   C0    40  41
          C1    42  43
          C2    44  45
          C3    46  47

是否有像df[(('A0','B1'),('A1','B0'),('A2','B1')),:]这样的索引方法?我已经尽力了,谢谢您的帮助.

Is there a indexing method like df[(('A0','B1'),('A1','B0'),('A2','B1')),:]? I have try my best, Thanks for any help.

推荐答案

您可以使用:

df1 = df.reset_index(level=2, drop=True)
mask = df1.index.isin(df1.groupby(level=[0])['b'].idxmax())
df = df[mask]

print (df)
                 a   b
lvl0 lvl1 lvl2        
A0   B1   C0     8   9
          C1    10  11
          C2    12  13
          C3    14  15
A1   B0   C0    16  47
          C1    18  49
          C2    20  41
          C3    22  43
A2   B1   C0    40  41
          C1    42  43
          C2    44  45
          C3    46  47

说明:

首先通过删除3级的MultiIndex. reset_index groupby idxmax 用于获取b列中最大值的索引:

First remove 3 level of MultiIndex by reset_index and groupby with idxmax for indices of max values in column b:

df1 = df.reset_index(level=2, drop=True)
idx = df1.groupby(level=[0])['b'].idxmax()
print (idx)
 lvl0
A0    (A0, B1)
A1    (A1, B0)
A2    (A2, B1)
Name: b, dtype: object

然后通过 :

print (df1.index.isin(idx))
[False False False False  True  True  True  True  True  True  True  True
 False False False False False False False False  True  True  True  True]

,最后通过 boolean indexing 进行过滤:

and last filter by boolean indexing:

df = df[df1.index.isin(idx)]
print (df)
                 a   b
lvl0 lvl1 lvl2        
A0   B1   C0     8   9
          C1    10  11
          C2    12  13
          C3    14  15
A1   B0   C0    16  47
          C1    18  49
          C2    20  41
          C3    22  43
A2   B1   C0    40  41
          C1    42  43
          C2    44  45
          C3    46  47

这篇关于在Pandas MultiIndex中获得特殊的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆