pandas 如何获取pandas MultiIndex数据框中的索引值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/22015363/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the index value in pandas MultiIndex data frame?
提问by colinfang
df = pd.DataFrame({'a':[2,3,5], 'b':[1,2,3], 'c':[12,13,14]})
df.set_index(['a','b'], inplace=True)
display(df)
s = df.iloc[1]
# How to get 'a' and 'b' value from s? 
It is so annoying that ones columns become indices we cannot simply use df['colname'] to fetch values.
令人讨厌的是,这些列变成了索引,我们不能简单地使用 df['colname'] 来获取值。
Does it encourage we use set_index(drop=False)?
它鼓励我们使用set_index(drop=False)吗?
回答by Wesley Bowman
When I print s I get
当我打印 s 我得到
In [8]: s = df.iloc[1]
In [9]: s
Out[9]:  
c    13
Name: (3, 2), dtype: int64
which has a and b in the name part, which you can access with:
其中名称部分有 a 和 b,您可以使用以下命令访问:
s.name
Something else that you can do is
你可以做的其他事情是
df.index.values
and specifically for your iloc[1]
并且专门针对您的 iloc[1]
df.index.values[1]
Does this help? Other than this I am not sure what you are looking for.
这有帮助吗?除此之外,我不确定您在寻找什么。
回答by ricmarchao
if you want to get "a" and "b"
如果你想得到“a”和“b”
df.index.names
df.index.names
gives: FrozenList(['a', 'b'])
给出:FrozenList(['a', 'b'])

