python / pandas从多索引中提取特定列

时间:2018-06-19 15:09:16

标签: python python-3.x pandas dataframe indexing

目前,我有一个如下的多重索引:

MultiIndex(levels=[[2012, 2013, 2014, 2015, 2016, 2017, 2018], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
           labels=[[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6], [10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4]],
           names=['Date', 'Date'])

我希望获得标签的第二部分,而不是[0,0,1,1],而是[10,11,0,1,2]本身。最好的方法是什么?

作为其他信息,我的数据框当前如下所示:

df = 

                Date  Portfolio Value    SPY Values
Date Date                                          
2012 11   2012-11-30     1.002448e+06  1.003667e+06
     12   2012-12-28     9.817744e+05  9.887664e+05
2013 1    2013-01-25     1.042036e+06  1.061184e+06
     2    2013-02-22     1.050522e+06  1.072771e+06
     3    2013-03-29     1.094525e+06  1.106260e+06
     4    2013-04-26     1.130510e+06  1.117988e+06
     5    2013-05-31     1.145836e+06  1.154727e+06
     6    2013-06-28     1.146044e+06  1.131836e+06
     7    2013-07-26     1.190659e+06  1.194927e+06
     8    2013-08-30     1.138982e+06  1.155645e+06
     9    2013-09-27     1.182957e+06  1.193585e+06
     10   2013-10-25     1.248856e+06  1.242970e+06
     11   2013-11-29     1.292239e+06  1.278790e+06
     12   2013-12-27     1.320085e+06  1.298785e+06
2014 1    2014-01-31     1.266189e+06  1.258302e+06
     2    2014-02-28     1.304377e+06  1.316448e+06
     3    2014-03-28     1.297855e+06  1.310442e+06
     4    2014-04-25     1.298360e+06  1.316306e+06 etc...

我希望从11、12、1、2、3等开始获得第二个Date索引。

1 个答案:

答案 0 :(得分:2)

您的MultiIndex标签是FrozenList个对象中的FrozenNDArray个。

您可以简单地通过常规列表索引访问第二个数组:

idx = pd.MultiIndex(levels=[[2012, 2013, 2014, 2015, 2016, 2017, 2018], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
                    labels=[[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6], [10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4]],
                    names=['Date', 'Date'])

res = idx.labels[1]

print(res)

FrozenNDArray([10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4], dtype='int8')