我有DataFrame和MultiIndex列,如下所示:
# sample data
col = pd.MultiIndex.from_arrays([['one', 'one', 'one', 'two', 'two', 'two'],
['a', 'b', 'c', 'a', 'b', 'c']])
data = pd.DataFrame(np.random.randn(4, 6), columns=col)
data
Run Code Online (Sandbox Code Playgroud)

['a', 'c']从第二级别仅选择特定列(例如,不是范围)的正确,简单方法是什么?
目前我这样做:
import itertools
tuples = [i for i in itertools.product(['one', 'two'], ['a', 'c'])]
new_index = pd.MultiIndex.from_tuples(tuples)
print(new_index)
data.reindex_axis(new_index, axis=1)
Run Code Online (Sandbox Code Playgroud)

然而,它不是一个好的解决方案,因为我必须淘汰itertools,手工构建另一个MultiIndex,然后重新索引(我的实际代码甚至更麻烦,因为列列表不是那么容易获取).我很确定必须有一些ix或xs这样做,但我尝试的一切都导致了错误.