24

I have a multi-index dataframe that looks like this:

 start      grad
 1995-96    1995-96 15  15  
            1996-97 6   6   
            2002-03 1   1   
            2007-08 1   1   

I'd like to drop by the specific values for the first level (level=0). In this case, I'd like to drop everything that has 1995-96 in the first index.

1
  • there are better ways, but: df.select(lambda row: row[0] != '1995-96', axis=0) Commented Aug 30, 2016 at 23:43

1 Answer 1

32

pandas.DataFrame.drop takes level as an optional argument

df.drop('1995-96', level='start')

As of v0.18.1, its docstring says:

"""
Signature: df.drop(labels, axis=0, level=None, inplace=False, errors='raise') 
Docstring: Return new object with labels in requested axis removed.

    Parameters
    ----------
    labels : single label or list-like
    axis : int or axis name
    level : int or level name, default None
        For MultiIndex
    inplace : bool, default False
        If True, do operation inplace and return None.
    errors : {'ignore', 'raise'}, default 'raise'
        If 'ignore', suppress error and existing labels are dropped.

    .. versionadded:: 0.16.1
"""
Sign up to request clarification or add additional context in comments.

2 Comments

If the first index had not been named as 'start', you could use level=0
Always best to use named index levels due to possible reordering.

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.