Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop specific rows from multiindex Dataframe

Tags:

python

pandas

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.

like image 427
micyukcha Avatar asked Aug 30 '16 23:08

micyukcha


1 Answers

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
"""
like image 66
Paul H Avatar answered Oct 19 '22 17:10

Paul H