Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:28:33+00:00 2026-06-10T15:28:33+00:00

I have a ‘df’ which have a multilevel index (STK_ID,RPT_Date) sales cogs net_pft STK_ID

  • 0

I have a ‘df’ which have a multilevel index (STK_ID,RPT_Date)

                       sales         cogs     net_pft
STK_ID RPT_Date                                      
000876 20060331          NaN          NaN         NaN
       20060630    857483000    729541000    67157200
       20060930   1063590000    925140000    50807000
       20061231    853960000    737660000    51574000
       20070331  -2695245000  -2305078000  -167642500
       20070630   1146245000   1050808000   113468500
       20070930   1327970000   1204800000    84337000
       20071231   1439140000   1331870000    53398000
       20080331  -3135240000  -2798090000  -248054300
       20080630   1932470000   1777010000   133756300
       20080930   1873240000   1733660000    92099000
002254 20061231 -16169620000 -15332705000  -508333200
       20070331   -763844000   -703460000    -1538000
       20070630    501221000    289167000   118012200
       20070930    460483000    274026000    95967000

How to write a command to filter the rows whose ‘RPT_Date’ contains ‘0630’ (which is the Q2 report) ? the result should be :

                       sales         cogs     net_pft
STK_ID RPT_Date                                      
000876 20060630    857483000    729541000    67157200
       20070630   1146245000   1050808000   113468500
       20080630   1932470000   1777010000   133756300
002254 20070630    501221000    289167000   118012200

I am trying to use df[df['RPT_Date'].str.contains('0630')], but Pandas refuses to work as 'RPT_Date' is not a column but a sub_level index.

Thanks for your tips …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:28:35+00:00Added an answer on June 10, 2026 at 3:28 pm

    To use the “str.*” methods on a column, you could reset the index, filter rows with a column “str.*” method call, and re-create the index.

    In [72]: x = df.reset_index(); x[x.RPT_Date.str.endswith("0630")].set_index(['STK_ID', 'RPT_Date'])
    Out[72]: 
                          sales        cogs    net_pft
    STK_ID RPT_Date                                   
    000876 20060630   857483000   729541000   67157200
           20070630  1146245000  1050808000  113468500
           20080630  1932470000  1777010000  133756300
    002254 20070630   501221000   289167000  118012200
    

    However, this approach is not particularly fast.

    In [73]: timeit x = df.reset_index(); x[x.RPT_Date.str.endswith("0630")].set_index(['STK_ID', 'RPT_Date'])
    1000 loops, best of 3: 1.78 ms per loop
    

    Another approach builds on the fact that a MultiIndex object behaves much like
    a list of tuples.

    In [75]: df.index
    Out[75]: 
    MultiIndex
    [('000876', '20060331') ('000876', '20060630') ('000876', '20060930')
     ('000876', '20061231') ('000876', '20070331') ('000876', '20070630')
     ('000876', '20070930') ('000876', '20071231') ('000876', '20080331')
     ('000876', '20080630') ('000876', '20080930') ('002254', '20061231')
     ('002254', '20070331') ('002254', '20070630') ('002254', '20070930')]
    

    Building on that, you can create a boolean array from a MultiIndex with df.index.map() and use the result to filter the frame.

    In [76]: df[df.index.map(lambda x: x[1].endswith("0630"))]
    Out[76]: 
                          sales        cogs    net_pft
    STK_ID RPT_Date                                   
    000876 20060630   857483000   729541000   67157200
           20070630  1146245000  1050808000  113468500
           20080630  1932470000  1777010000  133756300
    002254 20070630   501221000   289167000  118012200
    

    This is also quite a bit faster.

    In [77]: timeit df[df.index.map(lambda x: x[1].endswith("0630"))]
    1000 loops, best of 3: 240 us per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
Have two tables say ABC and XYZ and contain one column which data will
Have created a ATL COM project through which I am inserting Menu Items to
Have a linking (or ref) table which has a dual primary key. Need to
Have added designer extensions to EDMX designer which allow me to specify additional information
I have a text area in my form which accepts all possible characters from
Have a text box which get data for price. If someone enter something like
Have a bunch of classes which I need to do serialize and deserialize from/to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.