pandas.Series.drop#

Series.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')[源代码]#

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

根据指定的索引标签删除系列的元素。使用多索引时,可以通过指定级别来删除不同级别上的标签。

参数
labels单一标签或类似列表

要删除的索引标签。

axis0,默认为0

系列上的应用程序具有冗余性。

index单一标签或类似列表

系列上的应用程序是多余的,但可以使用‘index’而不是‘Labels’。

columns单一标签或类似列表

未对该系列进行任何更改;请改用‘index’或‘Labels’。

levelInt或Level名称,可选

对于多索引,为要删除其标签的级别。

inplace布尔值,默认为False

如果为True,则原地执行操作并返回None。

errors{‘忽略’,‘RAISE’},默认‘RAISE’

如果为‘Ignore’,则取消显示错误,并且只丢弃现有标签。

退货
系列或无

如果删除了指定的索引标签,则不删除序列 inplace=True

加薪
KeyError

如果在索引中未找到任何标签,则返回。

参见

Series.reindex

仅返回Series的指定索引标签。

Series.dropna

返回不带空值的序列。

Series.drop_duplicates

已删除重复值的返回系列。

DataFrame.drop

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

示例

>>> s = pd.Series(data=np.arange(3), index=['A', 'B', 'C'])
>>> s
A  0
B  1
C  2
dtype: int64

丢弃标签B en C

>>> s.drop(labels=['B', 'C'])
A  0
dtype: int64

丢弃多指数系列中的二级标签

>>> 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]])
>>> s = pd.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3],
...               index=midx)
>>> s
lama    speed      45.0
        weight    200.0
        length      1.2
cow     speed      30.0
        weight    250.0
        length      1.5
falcon  speed     320.0
        weight      1.0
        length      0.3
dtype: float64
>>> s.drop(labels='weight', level=1)
lama    speed      45.0
        length      1.2
cow     speed      30.0
        length      1.5
falcon  speed     320.0
        length      0.3
dtype: float64