pandas.Series.drop #

系列。drop ( labels = None , * , axis = 0 , index = None , columns = None , level = None , inplace = False , error = 'raise' ) [来源] #

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

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

参数
标签单个标签或类似列表

要删除的索引标签。

{0 或“索引”}

没用过。与 DataFrame 兼容所需的参数。

索引单个标签或类似列表

对于系列上的应用程序来说是多余的,但可以使用“索引”代替“标签”。

单标签或类似列表

系列没有任何改变;使用“索引”或“标签”代替。

level int 或级别名称,可选

对于 MultiIndex,将删除标签的级别。

inplace布尔值,默认 False

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

错误{'ignore', 'raise'}, 默认 'raise'

如果“忽略”,则抑制错误并且仅删除现有标签。

返回
系列或无

已删除指定索引标签的系列或 None if inplace=True

加薪
按键错误

如果在索引中没有找到任何标签。

也可以看看

Series.reindex

仅返回系列的指定索引标签。

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 和 C

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

删除多索引系列中的第二级标签

>>> midx = pd.MultiIndex(levels=[['llama', '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
llama   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)
llama   speed      45.0
        length      1.2
cow     speed      30.0
        length      1.5
falcon  speed     320.0
        length      0.3
dtype: float64