[英]Simple pandas MultiIndex slicing
I have the following DataFrame
: 我有以下
DataFrame
:
df_1 = DataFrame({
"alpha" : [1,1,1,2,2,2,3,3,3] ,
"beta" : [3,4,5,3,4,5,3,4,5] ,
"val_1" : ["x", "y" , "z", "w", "a", "b", "v1" , "v2" , "v3" ] ,
"val_2" : ["z1", "z2" , "z3", "w1", "w2", "w3" , "zz1" , "zz2" , "zz3" ]
})
df_1.set_index(["alpha", "beta"], inplace=True)
I am trying to select the following highlighted rows: 我试图选择以下突出显示的行:
That is, every row where beta
is either 3 or 5. 也就是说,
beta
每一行都是3或5。
I have gone through the pandas documentation multiple times and cannot find a way to do this. 我已多次浏览pandas文档,但无法找到解决方法。 The closest I've come to what I think must be the answer is:
我最接近我认为必须回答的是:
df_1.xs((3,5), level="beta", drop_level=False)
Which now currently fails. 现在哪个失败了。 What is the proper indexing/slicing way to get this?
得到这个的正确索引/切片方法是什么?
You can use the DF.query()
method to subset based on the specified values: 您可以使用
DF.query()
方法根据指定的值进行子集化:
df_1.query('beta == 3 or beta == 5') # More succintly : df_1.query('beta == [3,5]')
You can use pd.IndexSlice
. 您可以使用
pd.IndexSlice
。 There is a very similar example directly in the documentation. 文档中有一个非常类似的例子 。
df_1.loc[pd.IndexSlice[:, [3,5]], :]
val_1 val_2
alpha beta
1 3 x z1
5 z z3
2 3 w w1
5 b w3
3 3 v1 zz1
5 v3 zz3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.