【发布时间】:2016-08-26 01:38:36
【问题描述】:
如果我制作这样的多索引列数据框:
iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
index = pd.MultiIndex.from_product(iterables, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
first bar baz foo qux \
second one two one two one two one
A -0.119687 -0.518318 0.113920 -1.028505 1.106375 -1.020139 -0.039300
B 0.123480 -2.091120 0.464597 -0.147211 -0.489895 -1.090659 -0.592679
C -1.174376 0.282011 -0.197658 -0.030751 0.117374 1.591109 0.796908
first
second two
A -0.938209
B -0.851483
C 0.442621
我想使用列表仅从第一组列中选择列,
select_cols=['bar', 'qux']
这样的结果是:
first bar qux
second one two one two
A -0.119687 -0.518318 -0.039300 -0.938209
B 0.123480 -2.091120 -0.592679 -0.851483
C -1.174376 0.282011 0.796908 0.442621
我该怎么做呢? (提前谢谢)
【问题讨论】:
标签: python pandas dataframe multi-index