如何将 Pandas 列多索引名称作为列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34082137/
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 Pandas column multiindex names as a list
提问by neversaint
I have the following CSV data:
我有以下 CSV 数据:
id,gene,celltype,stem,stem,stem,bcell,bcell,tcell
id,gene,organs,bm,bm,fl,pt,pt,bm
134,foo,about_foo,20,10,11,23,22,79
222,bar,about_bar,17,13,55,12,13,88
And I can successfully summarize them this way:
我可以这样成功地总结它们:
import pandas as pd
df = pd.read_csv("http://dpaste.com/1X74TNP.txt",header=None,index_col=[1,2]).iloc[:, 1:]
df.columns = pd.MultiIndex.from_arrays(df.ix[:2].values)
df = df.ix[2:].astype(int)
df.index.names = ['cell', 'organ']
df = df.reset_index('organ', drop=True)
result = df.groupby(level=[0, 1], axis=1).mean()
result = result.stack().replace(np.nan, 0).unstack()
result = result.swaplevel(0,1, axis=1).sort_index(axis=1)
Which looks like:
看起来像:
In [341]: result
Out[341]:
bm fl pt
bcell stem tcell bcell stem tcell bcell stem tcell
cell
foo 0 15 79 0 11 0 22.5 0 0
bar 0 15 88 0 55 0 12.5 0 0
My question is, from result
how can I get the column index of the first level as list:
我的问题是,result
如何从列表中获取第一级的列索引:
['bm','fl','pt']
回答by Liam Foley
result.columns
returns a pandas.core.index.MultiIndex
which has a levels attribute.
result.columns
返回一个pandas.core.index.MultiIndex
具有 levels 属性的。
list(result.columns.levels[0])
returns
回报
['bm', 'fl', 'pt']
回答by Thiru kumaran
Additionally you could use columnns.get_level_values(level)
另外你可以使用 columnns.get_level_values(level)
>>> result.columns.get_level_values(0).unique()
array(['bm', 'fl', 'pt'], dtype=object)
>>> list(result.columns.get_level_values(0))
['bm', 'bm', 'bm', 'fl', 'fl', 'fl', 'pt', 'pt', 'pt']