lee.edward01
lee.edward01

Reputation: 473

python/pandas pull specific column from multi-index

Currently I have a multi-index as follows:

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'])

I was hoping to the get the second part of the labels, not the [0,0,1,1] but the [10,11,0,1,2] by itself. What would be the best possible way to do this?

As additional information my dataframe currently looks like this

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...

and I was hoping to get the second Date index, starting from 11, 12, 1, 2, 3, etc.

Upvotes: 1

Views: 60

Answers (1)

jpp
jpp

Reputation: 164673

Your MultiIndex labels are a FrozenList of FrozenNDArray objects.

You can simply access the second array via regular list indexing:

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')

Upvotes: 2

Related Questions