使用MultiIndex访问Pandas DataFrame时的KeyError

时间:2015-10-24 16:32:01

标签: python python-2.7 pandas

pandas.DataFrame已创建MultiIndex,如下所示:

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three three two four three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})

df = df.set_index(['A','B'])

这会创建一个`DataFrame``:

           C   D
A   B           
foo one    0   0
bar one    1   2
foo two    2   4
bar three  3   6
foo three  4   8
bar two    5  10
foo four   6  12
    three  7  14

问题:为什么尝试使用KeyError: 'foo'进行选择时会获得df['foo']?同样,df['foo', 'one']df['foo']['one']

此外,MultiIndex没有将所有foo组合在A下?是否有必要将它们组合在一起,例如:

          A         B
one 1 -0.732470 -0.313871
    2 -0.031109 -2.068794
    3  1.520652  0.471764
two 1 -0.101713 -1.204458
    2  0.958008 -0.455419
    3 -0.191702 -0.915983

1 个答案:

答案 0 :(得分:1)

df['foo']尝试选择列foo,因此生成KeyError,因为没有foo列。我想你打算做df.loc['foo']df.loc['foo', 'one']