drop(labels, axis)

本文详细介绍了 Pandas 中的 drop() 方法,该方法用于从 DataFrame 或 Series 中删除指定的行或列。通过实例演示了如何使用参数如 labels、axis、index、columns 和 inplace 来精确控制数据的删除过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

def drop(self, labels=None, axis=0, index=None, columns=None, level=None,
         inplace=False, errors='raise'):
    """
    Return new object with labels in requested axis removed.

    Parameters
    ----------
    labels : single label or list-like
        Index or column labels to drop.
axis=0axis=1
删除行删除列

axis : int or axis name
Whether to drop labels from the index (0 / ‘index’) or
columns (1 / ‘columns’).

    index, columns : single label or list-like
        Alternative to specifying `axis` (``labels, axis=1`` is
        equivalent to ``columns=labels``).

        .. versionadded:: 0.21.0
    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.

    Returns
    -------
    dropped : type of caller

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

    Notes
    -----
    Specifying both `labels` and `index` or `columns` will raise a
    ValueError.

    """
eyError Traceback (most recent call last) Cell In[48], line 14 12 iris.head(20) 13 #删除列class ---> 14 iris.drop('class', axis=1, inplace=True) File D:\Anaconda\Lib\site-packages\pandas\core\frame.py:5581, in DataFrame.drop(self, labels, axis, index, columns, level, inplace, errors) 5433 def drop( 5434 self, 5435 labels: IndexLabel | None = None, (...) 5442 errors: IgnoreRaise = "raise", 5443 ) -> DataFrame | None: 5444 """ 5445 Drop specified labels from rows or columns. 5446 (...) 5579 weight 1.0 0.8 5580 """ -> 5581 return super().drop( 5582 labels=labels, 5583 axis=axis, 5584 index=index, 5585 columns=columns, 5586 level=level, 5587 inplace=inplace, 5588 errors=errors, 5589 ) File D:\Anaconda\Lib\site-packages\pandas\core\generic.py:4788, in NDFrame.drop(self, labels, axis, index, columns, level, inplace, errors) 4786 for axis, labels in axes.items(): 4787 if labels is not None: -> 4788 obj = obj._drop_axis(labels, axis, level=level, errors=errors) 4790 if inplace: 4791 self._update_inplace(obj) File D:\Anaconda\Lib\site-packages\pandas\core\generic.py:4830, in NDFrame._drop_axis(self, labels, axis, level, errors, only_slice) 4828 new_axis = axis.drop(labels, level=level, errors=errors) 4829 else: -> 4830 new_axis = axis.drop(labels, errors=errors) 4831 indexer = axis.get_indexer(new_axis) 4833 # Case for non-unique axis 4834 else: File D:\Anaconda\Lib\site-packages\pandas\core\indexes\base.py:7070, in Index.drop(self, labels, errors) 7068 if mask.any(): 7069 if errors != "ignore": -> 7070 raise KeyError(f"{labels[mask].tolist()} not found in axis") 7071 indexer = indexer[~mask] 7072 return self.delete(indexer) KeyError: "['class'] not found in axis"
03-26
KeyError Traceback (most recent call last) Cell In[17], line 1 ----> 1 data = data.drop(['125','125.1'],axis=1) 2 data File D:\anaconda\envs\zuoye\lib\site-packages\pandas\core\frame.py:5268, in DataFrame.drop(self, labels, axis, index, columns, level, inplace, errors) 5120 def drop( 5121 self, 5122 labels: IndexLabel = None, (...) 5129 errors: IgnoreRaise = "raise", 5130 ) -> DataFrame | None: 5131 """ 5132 Drop specified labels from rows or columns. 5133 (...) 5266 weight 1.0 0.8 5267 """ -> 5268 return super().drop( 5269 labels=labels, 5270 axis=axis, 5271 index=index, 5272 columns=columns, 5273 level=level, 5274 inplace=inplace, 5275 errors=errors, 5276 ) File D:\anaconda\envs\zuoye\lib\site-packages\pandas\core\generic.py:4549, in NDFrame.drop(self, labels, axis, index, columns, level, inplace, errors) 4547 for axis, labels in axes.items(): 4548 if labels is not None: -> 4549 obj = obj._drop_axis(labels, axis, level=level, errors=errors) 4551 if inplace: 4552 self._update_inplace(obj) File D:\anaconda\envs\zuoye\lib\site-packages\pandas\core\generic.py:4591, in NDFrame._drop_axis(self, labels, axis, level, errors, only_slice) 4589 new_axis = axis.drop(labels, level=level, errors=errors) 4590 else: -> 4591 new_axis = axis.drop(labels, errors=errors) 4592 indexer = axis.get_indexer(new_axis) 4594 # Case for non-unique axis 4595 else: File D:\anaconda\envs\zuoye\lib\site-packages\pandas\core\indexes\base.py:6696, in Index.drop(self, labels, errors) 6694 if mask.any(): 6695 if errors != "ignore": -> 6696 raise KeyError(f"{list(labels[mask])} not found in axis") 6697 indexer = indexer[~mask] 6698 return self.delete(indexer) KeyError: "['125', '125.1'] not found in axis"
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值