1

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?

2
  • it means df['RPT_Date'] can not fetch the RPT_Date column's values. Actually, python return "KeyError: 'no item named RPT_Date'" Commented Aug 23, 2012 at 12:54
  • 'RPT_Date' is not a column of your DataFrame, so it is normal that this throws a KeyError. 'RPT_Date' is a level in the index, not accessible by the usual column indexing. Commented Aug 23, 2012 at 13:17

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

That would work if you did:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.