4

In the following DataFrame there are a 2-level MultiIndex, namely city and date:

                 temp
                count
city date            
SFO  2014-05-31    31
     2014-06-30    30
     2014-07-31    31
     2014-08-31    31
     2014-09-30    30
YYZ  2014-05-31    31
     2014-06-30    30
     2014-07-31    31
     2014-08-31    31
     2014-09-30    30

I want to drop 2014-05-31 and 2014-09-30 from the date level.

How do I do this?

Comment: To build the DataFrame -

df = pd.DataFrame(
    {('temp', 'count'): {('SFO', Timestamp('2014-05-31 00:00:00')): 31,
                         ('SFO', Timestamp('2014-06-30 00:00:00')): 30,
                         ('SFO', Timestamp('2014-07-31 00:00:00')): 31,
                         ('SFO', Timestamp('2014-08-31 00:00:00')): 31,
                         ('SFO', Timestamp('2014-09-30 00:00:00')): 30,
                         ('YYZ', Timestamp('2014-05-31 00:00:00')): 31,
                         ('YYZ', Timestamp('2014-06-30 00:00:00')): 30,
                         ('YYZ', Timestamp('2014-07-31 00:00:00')): 31,
                         ('YYZ', Timestamp('2014-08-31 00:00:00')): 31,
                         ('YYZ', Timestamp('2014-09-30 00:00:00')): 30}}
).rename_axis(['city','date'])

1 Answer 1

6

You can give drop a specific level:

In[4]: df.drop([Timestamp('2014-05-31'),Timestamp('2014-09-30')],level=1)
Out[4]: 
                 temp
                count
city date            
SFO  2014-06-30    30
     2014-07-31    31
     2014-08-31    31
YYZ  2014-06-30    30
     2014-07-31    31
     2014-08-31    31
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.