pandas 从多索引数据框中删除特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39238639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:55:34  来源:igfitidea点击:

Drop specific rows from multiindex Dataframe

pythonpandas

提问by micyukcha

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.

我想删除第一级(级别 = 0)的特定值。在这种情况下,我想删除第一个索引中包含 1995-96 的所有内容。

回答by Paul H

pandas.DataFrame.droptakes level as an optional argument

pandas.DataFrame.drop将 level 作为可选参数

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

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

As of v0.18.1, its docstring says:

从 v0.18.1 开始,它的文档字符串说:

"""
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
"""
"""
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
"""