pandas 仅在多索引中的第二个索引上使用 .loc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50413993/
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
Using .loc on just second index in multiindex
提问by snapcrack
I have multiindex dataframe that looks like this:
我有如下所示的多索引数据框:
value
year name
1921 Ah 40
1921 Ai 90
1922 Ah 100
1922 Ai 7
in which year
and name
are the indices. I want to select every row where the name Ai
appears. I have tried df.loc[(:,'Ai')]
and df.loc['Ai']
but both give errors. How do I index only using the name column?
其中year
和name
是指数。我想选择名称Ai
出现的每一行。我试过df.loc[(:,'Ai')]
,df.loc['Ai']
但都出现错误。如何仅使用名称列进行索引?
采纳答案by sacuL
I would use .xs
on the first level of your multiindex (note: level=1
refers to the "second" index (name
) because of python's zero indexing: level 0 is year
in your case):
我会.xs
在你的多重level=1
索引的第一级使用(注意:指的是“第二”索引 ( name
) 因为 python 的零索引:级别 0year
在你的情况下):
df.xs('Ai', level=1, drop_level=False)
# or
df.xs('Ai', level='name', drop_level=False)
value
year name
1921 Ai 90
1922 Ai 7
回答by cs95
@sacul has the most idiomatic answer, but here are a few alternatives.
@sacul 有最惯用的答案,但这里有一些替代方案。
MultiIndex.get_level_values
MultiIndex.get_level_values
df[df.index.get_level_values('name') == 'Ai']
value
year name
1921 Ai 90
1922 Ai 7
DataFrame.query
DataFrame.query
df.query('name == "Ai"')
value
year name
1921 Ai 90
1922 Ai 7
DataFrame.loc(axis=0)
with pd.IndexSlice
DataFrame.loc(axis=0)
和 pd.IndexSlice
Similar to @liliscent's answer, but does not need the trailing :
if you specify axis=0
.
类似于@liliscent 的答案,但:
如果您指定axis=0
.
df.loc(axis=0)[pd.IndexSlice[:, 'Ai']]
value
year name
1921 Ai 90
1922 Ai 7
回答by llllllllll
If you prefer loc
, you can use:
如果您愿意loc
,可以使用:
In [245]: df.loc[(slice(None), 'Ai'), :]
...:
Out[245]:
value
year name
1921 Ai 90
1922 Ai 7