Drop specific rows from a multi-index Dataframe

I have a multi-index framework 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 would like to remove certain values ​​for the first level (level = 0). In this case, I would like to leave everything that was in 1995-1996 in the first index.

+4
source share
1 answer

pandas.DataFrame.drop takes level as an optional argument

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

Starting with v0.18.1, its docstron 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
"""
+7
source

Source: https://habr.com/ru/post/1653069/


All Articles