[英]Dataframe access second level of MultiIndex
I have been trying to figure out the best way to deal with a multiIndex. 我一直在试图找出处理multiIndex的最佳方法。 Especially when I want to access some values on the second level of the multiIndex.
特别是当我想在multiIndex的第二级访问某些值时。 For example:
例如:
df = pd.DataFrame([np.random.randint(3,size=20),np.random.rand(20)]).T
df.columns = ['A','B']
g = df.groupby('A').describe()
Let's say I'm trying to look at the mean values of this output. 假设我试图查看此输出的平均值。 I can do something like this:
我可以这样做:
idx = [True if a[1]=='mean' else False for a in g.index.tolist()]
p.loc[idx,:]
It works but there must be a better way to do this. 它有效但必须有更好的方法来做到这一点。 Is there a better way to access the second level multiIndex?
有没有更好的方法来访问二级multiIndex?
I found a couple of easy answers: 我找到了几个简单的答案:
g.xs('mean', level=1)
Another one: 另一个:
idx = pd.IndexSlice
g.loc[idx[:,'mean'],:]
You should read over the documentation on multi-index data frames. 您应该阅读有关多索引数据框的文档。 IndexSlice is the way to handle this.
IndexSlice是处理这个问题的方法。 Something like this should work.
这样的事情应该有效。
import pandas as pd
idx = pd.IndexSlice
g.loc[idx[:,"mean"],:]
You can swap the order of indexes in the multiindex: 您可以在多索引中交换索引的顺序:
g.reorder_levels([1,0]).loc['mean']
# B
#A
#0.0 0.515745
#1.0 0.451534
#2.0 0.483014
You can also do this 你也可以这样做
g.loc[:, ("B", "mean")]
A
0.0 0.381882
1.0 0.450356
2.0 0.497692
Name: (B, mean), dtype: float64
Check out advanced indexing with hierarchical index 使用分层索引检查高级索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.