Python 熊猫数据框选择多索引中的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25189575/
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
pandas dataframe select columns in multiindex
提问by wfh
I have the following pd.DataFrame:
我有以下 pd.DataFrame:
Name 0 1 ...
Col A B A B ...
0 0.409511 -0.537108 -0.355529 0.212134 ...
1 -0.332276 -1.087013 0.083684 0.529002 ...
2 1.138159 -0.327212 0.570834 2.337718 ...
It has MultiIndex columns with names=['Name', 'Col']and hierarchical levels. The Namelabel goes from 0 to n, and for each label, there are two Aand Bcolumns.
它具有带有names=['Name', 'Col']和 分层级别的MultiIndex 列。该Name标签从0到n,并为每个标签,有两个A和B列。
I would like to subselect all the A(or B) columns of this DataFrame.
我想子选择此 DataFrame 的所有A(或B)列。
采纳答案by CT Zhu
There is a get_level_valuesmethod that you can use in conjunction with boolean indexing to get the the intended result.
有一种get_level_values方法可以与布尔索引结合使用来获得预期的结果。
In [13]:
df = pd.DataFrame(np.random.random((4,4)))
df.columns = pd.MultiIndex.from_product([[1,2],['A','B']])
print df
1 2
A B A B
0 0.543980 0.628078 0.756941 0.698824
1 0.633005 0.089604 0.198510 0.783556
2 0.662391 0.541182 0.544060 0.059381
3 0.841242 0.634603 0.815334 0.848120
In [14]:
print df.iloc[:, df.columns.get_level_values(1)=='A']
1 2
A A
0 0.543980 0.756941
1 0.633005 0.198510
2 0.662391 0.544060
3 0.841242 0.815334
回答by ZJS
EDIT* Best way now is to use indexSlice for multi-index selections
编辑* 现在最好的方法是使用 indexSlice 进行多索引选择
idx = pd.IndexSlice
A = df.loc[:,idx[:,'A']]
B = df.loc[:,idx[:,'B']]
回答by user2725109
Method 1:
方法一:
df.xs('A', level='Col', axis=1)
for more refer to http://pandas.pydata.org/pandas-docs/stable/advanced.html#cross-section
有关更多信息,请参阅http://pandas.pydata.org/pandas-docs/stable/advanced.html#cross-section
Method 2:
方法二:
df.loc[:, (slice(None), 'A')]
Caveat:this method requires the labels to be sorted. for more refer to http://pandas.pydata.org/pandas-docs/stable/advanced.html#the-need-for-sortedness-with-multiindex
警告:此方法需要对标签进行排序。有关更多信息,请参阅http://pandas.pydata.org/pandas-docs/stable/advanced.html#the-need-for-sortedness-with-multiindex

