简体   繁体   English

部分 label 索引上的 Pandas MultiIndexing KeyError

[英]Pandas MultiIndexing KeyError on partial label index

I'm trying to reproduce the "Basic indexing on axis with MultiIndex" portion of the pandas user guide .我正在尝试重现 pandas 用户指南的“使用 MultiIndex 轴上的基本索引”部分。

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
          ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
print(df)
print(df['bar'])

I can't see why my code is any different from what is shown in the user guide.我看不出为什么我的代码与用户指南中显示的代码有任何不同。 I have a KeyError: 'bar' in the last line.我有一个 KeyError: 'bar' 在最后一行。

If you want to access bar you need to use loc , or transpose it, because bar is not in columns:如果要访问bar ,则需要使用loc或转置它,因为bar不在列中:

>>> df.loc['bar']
            0         1         2         3
one  0.888182  0.066730  1.397408 -0.550522
two -0.258916 -1.859689 -0.294348 -0.646791

transpose:转置:

>>> df.T['bar']

        one       two
0  0.888182 -0.258916
1  0.066730 -1.859689
2  1.397408 -0.294348
3 -0.550522 -0.646791

EDIT:编辑:

As pointed out, very helpfully, in the comments, in case you want to keep the multiindex:正如所指出的,在评论中非常有帮助,如果您想保留多索引:

>>> df.xs('bar',drop_level=False)
                0         1         2         3
bar one -0.857271  1.271094  0.565691 -0.523375
    two  0.826911  0.244787  0.991158 -0.484815

Even though it will depend on your use-case, seems like you actually need:尽管这取决于您的用例,但您似乎确实需要:

>>> arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
          ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
>>> df = pd.DataFrame(np.random.randn(4, 8), columns=arrays)

>>> df

        bar                 baz                 foo                 qux          
        one       two       one       two       one       two       one       two
0  1.600817 -1.420187 -0.798078  1.632550 -0.737740 -1.036077 -1.034157  1.576907
1  0.111148 -1.830283  0.507195 -0.042425  0.260859 -1.600065 -0.449921  0.657582
2 -1.054305 -0.885309  0.325678 -0.253772 -0.444176  0.331933  0.332281  0.127738
3  1.071590  0.947280 -0.973616  0.677141  0.133742  1.352731 -0.210731  2.079073

# Then you can do either df['bar'] directly

References:参考:

  1. df.xs

  2. df.T

  3. df.loc

  4. The link in your question.您问题中的链接。 I would suggest you to go through it thoroughly, it is very well explained.我建议您彻底阅读go,它很好地解释了。

The part of the user guide you are referring to ( this ) is for when the dataframe's columns are multiindex.您所指的用户指南部分( this )适用于数据框的列是多索引时。 You should look at this part for when the index is multiindex.当索引是多索引时,您应该查看这部分

To access bar , you should do:要访问bar ,您应该执行以下操作:

print(df.loc["bar"])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM