python——drop函数

该函数用于从数据帧中删除指定的行或列标签。可以按标签名称、轴(行或列)、索引或列名进行删除。对于多索引,还可以指定删除的级别。操作可以原地执行,也可以返回新数据帧。如果标签不存在,会引发KeyError,除非设置`errors='ignore'`。函数提供了多种示例,包括删除单个或多个列、按索引删除行,以及在多索引数据帧中的删除操作。
摘要由CSDN通过智能技术生成

函数定义:

​
def drop(
        self,
        labels=None,
        axis: Axis = 0,
        index=None,
        columns=None,
        level: Level | None = None,
        inplace: bool = False,
        errors: str = "raise",
    ):

注解:

 """
        Drop specified labels from rows or columns.

        Remove rows or columns by specifying label names and corresponding
        axis, or by specifying directly index or column names. When using a
        multi-index, labels on different levels can be removed by specifying
        the level. See the `user guide <advanced.shown_levels>`
        for more information about the now unused levels.

        Parameters
        ----------
        labels : single label or list-like
            Index or column labels to drop. A tuple will be used as a single
            label and not treated as a list-like.
        axis : {0 or 'index', 1 or 'columns'}, default 0
            Whether to drop labels from the index (0 or 'index') or
            columns (1 or 'columns').
        index : single label or list-like
            Alternative to specifying axis (``labels, axis=0``
            is equivalent to ``index=labels``).
        columns : single label or list-like
            Alternative to specifying axis (``labels, axis=1``
            is equivalent to ``columns=labels``).
        level : int or level name, optional
            For MultiIndex, level from which the labels will be removed.
        inplace : bool, default False
            If False, return a copy. Otherwise, do operation
            inplace and return None.
        errors : {'ignore', 'raise'}, default 'raise'
            If 'ignore', suppress error and only existing labels are
            dropped.

        Returns
        -------
        DataFrame or None
            DataFrame without the removed index or column labels or
            None if ``inplace=True``.

        Raises
        ------
        KeyError
            If any of the labels is not found in the selected axis.

        See Also
        --------
        DataFrame.loc : Label-location based indexer for selection by label.
        DataFrame.dropna : Return DataFrame with labels on given axis omitted
            where (all or any) data are missing.
        DataFrame.drop_duplicates : Return DataFrame with duplicate rows
            removed, optionally only considering certain columns.
        Series.drop : Return Series with specified index labels removed.
"""

从行或列中删除指定的标签。



通过指定标签名称和相应的

或者直接指定索引或列名。当使用

多索引、不同级别的标签可以通过指定

水平。请参阅“用户指南<高级”。显示的\u级别>`

有关现在未使用的级别的更多信息。



参数

----------

标签:单个标签或类似列表

要删除的索引或列标签。元组将用作单个

标签,而不是像列表一样处理。

轴:{0或'index',1或'columns',默认为0

是从索引(0或“索引”)中删除标签,还是

列(1或“列”)。

索引:单个标签或类似列表

指定轴的替代方法(`labels,axis=0)``

相当于``index=labels`)。

列:单个标签或类似列表

指定轴的替代方法(`labels,axis=1)``

相当于``columns=labels`)。

级别:int或级别名称,可选

对于多索引,将从中删除标签的级别。

原地:bool,默认为False

如果为False,请返回一份副本。否则,请执行操作

原地踏步,一无所获。

错误:{'ignore','raise'},默认值为'raise'

如果为“忽略”,则抑制错误,并且仅显示现有标签

下降。



返回

-------

数据帧或无

没有移除索引或列标签的DataFrame或

如果“`inplace=True`”,则为无。



提高

------

键错误

如果在选定轴中找不到任何标签。



另见

--------

数据帧。loc:基于标签位置的索引器,用于按标签进行选择。

数据帧。dropna:返回给定轴上省略标签的数据帧

(全部或任何)数据缺失的地方。

数据帧。drop_duplicates:返回具有重复行的数据帧

删除,也可以仅考虑某些列。

系列删除:删除指定索引标签的返回序列。

 例子:

--------
        >>> df = pd.DataFrame(np.arange(12).reshape(3, 4),
        ...                   columns=['A', 'B', 'C', 'D'])
        >>> df
           A  B   C   D
        0  0  1   2   3
        1  4  5   6   7
        2  8  9  10  11
      Drop columns

        >>> df.drop(['B', 'C'], axis=1)
           A   D
        0  0   3
        1  4   7
        2  8  11

        >>> df.drop(columns=['B', 'C'])
           A   D
        0  0   3
        1  4   7
        2  8  11

      
  Drop a row by index

        >>> df.drop([0, 1])
           A  B   C   D
        2  8  9  10  11

        Drop columns and/or rows of MultiIndex DataFrame

        >>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
        ...                              ['speed', 'weight', 'length']],
        ...                      codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
        ...                             [0, 1, 2, 0, 1, 2, 0, 1, 2]])
        >>> df = pd.DataFrame(index=midx, columns=['big', 'small'],
        ...                   data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
        ...                         [250, 150], [1.5, 0.8], [320, 250],
        ...                         [1, 0.8], [0.3, 0.2]])
        >>> df
                        big     small
        lama    speed   45.0    30.0
                weight  200.0   100.0
                length  1.5     1.0
        cow     speed   30.0    20.0
                weight  250.0   150.0
                length  1.5     0.8
        falcon  speed   320.0   250.0
                weight  1.0     0.8
                length  0.3     0.2

        Drop a specific index combination from the MultiIndex
        DataFrame, i.e., drop the combination ``'falcon'`` and
        ``'weight'``, which deletes only the corresponding row

        >>> df.drop(index=('falcon', 'weight'))
                        big     small
        lama    speed   45.0    30.0
                weight  200.0   100.0
                length  1.5     1.0
        cow     speed   30.0    20.0
                weight  250.0   150.0
                length  1.5     0.8
        falcon  speed   320.0   250.0
                length  0.3     0.2

        >>> df.drop(index='cow', columns='small')
                        big
        lama    speed   45.0
                weight  200.0
                length  1.5
        falcon  speed   320.0
                weight  1.0
                length  0.3

        >>> df.drop(index='length', level=1)
                        big     small
        lama    speed   45.0    30.0
                weight  200.0   100.0
        cow     speed   30.0    20.0
                weight  250.0   150.0
        falcon  speed   320.0   250.0
                weight  1.0     0.8
很好,以下是关于 pandas 预处理的答案: pandas 是一种基于 NumPy 的数据分析工具,提供了大量的函数和方法来处理数据。在数据分析中,预处理数据是非常重要的一步,pandas 提供了多种方法来进行数据预处理,包括数据清洗、缺失值处理、重复值处理、数据转换等。 1. 数据清洗 数据清洗是指对数据进行初步处理,去除不必要的信息和异常值。pandas 提供了多种方法来进行数据清洗,包括: - drop_duplicates:去除重复值。 - dropna:去除缺失值。 - replace:替换指定的值。 - fillna:填充缺失值。 - drop:删除指定的行或列。 2. 缺失值处理 缺失值是指数据中的某些值缺失或为空,pandas 提供了多种方法来处理缺失值,包括: - isnull:判断某个值是否为空。 - notnull:判断某个值是否不为空。 - dropna:删除包含缺失值的行或列。 - fillna:填充缺失值。 3. 重复值处理 重复值是指数据中的某些值出现了重复,pandas 提供了多种方法来处理重复值,包括: - duplicated:判断某个值是否出现过重复。 - drop_duplicates:删除重复值。 4. 数据转换 数据转换是指将数据转换成适合分析的格式或类型,pandas 提供了多种方法来进行数据转换,包括: - astype:将某列数据转换成指定的数据类型。 - apply:对某个列或行应用指定的函数。 - map:对某个列应用指定的映射关系。 - pivot_table:对数据进行透视操作。 以上就是 pandas 预处理的答案,希望对你有所帮助。如果还有其他问题,可以继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙有肥鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值