访问多索引数据框中的列

时间:2020-04-29 15:33:00

标签: python pandas multi-index

一天之内的第二个问题,我希望这很好...我仍然花很多时间尝试自己解决这个问题,并在使用此网站之前在互联网上看很多东西。

无论如何,我有一张这样的桌子:

index = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
         np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
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       two
A       0.895717  0.805244 -1.206412  2.565646  1.431256  1.340309 -1.170299 -0.226169
B       0.410835  0.813850  0.132003 -0.827317 -0.076467 -1.187678  1.130127 -1.436737
C      -1.413681  1.607920  1.024180  0.569605  0.875906 -2.211372  0.974466 -2.006747

要访问列索引栏/一个,我可以运行df['bar', 'one']df['bar']['one'] 但是我想使用诸如 columns = ['bar', 'one'] 接着 df[columns] 但这不起作用:"['one'] not in index" 有什么想法吗?

请清楚一点,我想通过一个操作访问索引,而不必为不同的列进行更改:df[columns]columns应该是什么?

0 个答案:

没有答案
相关问题