bigbug
bigbug

Reputation: 59554

How to get one series within a multilevel index in python pandas

I have a data frame 'df' which has a multilevel index ('STK_ID','RPT_Date'):

    sales        cogs    net_pft
STK_ID RPT_Date                                   
600809 20120331  2214010000   509940000  492532000
       20111231  4488150000  1077190000  780547000
       20110930  3563660000   850789000  707537000
       20110630  2894820000   703883000  658625000

Some code:

>>> df.index.names
['STK_ID', 'RPT_Date']

now I want to get the RPT_Date column's series value (20120331,20111231,20110930,20110630) by:

>>> df['RPT_Date'] # not work

How do I get that data?

Upvotes: 1

Views: 1699

Answers (2)

bigbug
bigbug

Reputation: 59554

I fixed it.

df.index.get_level_values('RPT_Date')

array([20120331, 20111231, 20110930, 20110630, 20110331, 20101231,
       20100930, 20100630, 20100331, 20091231, 20090930, 20090630,
       20090331, 20081231, 20080930, 20080630, 20080331, 20071231,
       20070930, 20070630, 20070331, 20061231, 20060930, 20060630,
       20060331], dtype=object)

I find the syntax quite ugly, why does the developer of Pandas not accept the straightforward way of df['RPT_Date']?

Upvotes: 2

Wes McKinney
Wes McKinney

Reputation: 105661

That would work if you did:

df2 = df.reset_index()
df2['RPT_Date']

Upvotes: 1

Related Questions