具有多索引的邻居列值的DataFrame切片

时间:2018-03-10 15:30:26

标签: python pandas multi-index

最近,我开始使用pandas进行简单的数据分析任务。所以问题可能是愚蠢的,但我不得不问它。

我的数据如下:

                0                   1                   2 
                ix    increasing    ix     increasing   ix    increasing
noname  vtg                         
name1   -0.500  0.000109    True    0.000158    False   0.000153    True
        -0.498  0.000130    True    0.000154    False   0.000158    True
        -0.496  0.000141    True    0.000153    False   0.000158    True
        -0.494  0.000124    True    0.000154    False   0.000154    True
        -0.492  0.000109    True    0.000151    False   0.000154    True

我需要和' ix'列如果增加'相应的键值为True。

我发现这只是一种丑陋的方式:

idx = pd.IndexSlice
qq = data.loc[idx[::, idx[::, 'increasing']]].apply(lambda x: True if all(x) else False, axis=0)

然后

data.loc[idx[::, idx[list(qq.loc[qq==True].index.labels[0]), 'ix']]]

给出所有列' ix'真正的价值在增加'。我想应该有一个像这样做的熊猫。

提前致谢!

1 个答案:

答案 0 :(得分:1)

使用xs进行选择可能会使用all来检查每列的所有True

qq = data.xs('increasing', axis=1, level=1).all()
print (qq)
0     True
1    False
2     True
dtype: bool

然后选择第二级并按loc过滤:

df = data.xs('ix', axis=1, level=1).loc[:, qq]
print (df)
                     0         2
name1 -0.500  0.000109  0.000153
      -0.498  0.000130  0.000158
      -0.496  0.000141  0.000158
      -0.494  0.000124  0.000154
      -0.492  0.000109  0.000154

如果列中也需要MultiIndex

df = data.xs('ix', axis=1, level=1, drop_level=False).loc[:, qq.values]
print (df)
                     0         2
                    ix        ix
name1 -0.500  0.000109  0.000153
      -0.498  0.000130  0.000158
      -0.496  0.000141  0.000158
      -0.494  0.000124  0.000154
      -0.492  0.000109  0.000154
相关问题