Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 基于子层次的层次索引_Pandas - Fatal编程技术网

Pandas 基于子层次的层次索引

Pandas 基于子层次的层次索引,pandas,Pandas,我有一个如下的数据帧。如何选择第二个索引位于['two','twree']中的行 index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two', 'three']], labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [

我有一个如下的数据帧。如何选择第二个索引位于
['two','twree']
中的行

index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
                               ['one', 'two', 'three']],
                       labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
                               [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]])
hdf = DataFrame(np.random.randn(10, 3), index=index,
            columns=['A', 'B', 'C'])

In [3]: hdf
Out[3]: 
                  A         B         C
foo one   -1.274689  0.946294 -0.149131
    two   -0.015483  1.630099  0.085461
    three  1.396752 -0.272583 -0.760000
bar one   -1.151217  1.269658  2.457231
    two   -1.657258 -1.271384 -2.429598
baz two    1.124609  0.138720 -1.994984
    three  0.124298 -0.127099 -0.409736
qux one    0.535038  1.139026  0.414842
    two    0.287724  0.461041 -0.268918
    three -0.259649  0.226574 -0.558334

使用DataFrame方法的一种方法是:

注意:您还可以执行以下操作:

In [9]: hdf.index.get_level_values(1).isin(['two', 'three'])
Out[9]: array([False,  True,  True, False,  True,  True,  True, False,  True,  True], dtype=bool)
对于这一点,应该有更好的语法

In [9]: hdf.index.get_level_values(1).isin(['two', 'three'])
Out[9]: array([False,  True,  True, False,  True,  True,  True, False,  True,  True], dtype=bool)