pyspark.pandas.Series.drop

Series. drop ( labels : Union[Any, Tuple[Any, …], List[Union[Any, Tuple[Any, …]]], None] = None , index : Union[Any, Tuple[Any, …], List[Union[Any, Tuple[Any, …]]], None] = None , columns : Union[Any, Tuple[Any, …], List[Union[Any, Tuple[Any, …]]], None] = None , level : Optional [ int ] = None , inplace : bool = False ) → pyspark.pandas.series.Series [source]

返回移除了指定索引标签的序列。

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

Parameters
labels single label or list-like

要删除的索引标签。

index single label or list-like

在Series上应用是多余的,但可以使用索引代替标签。

columns single label or list-like

不对 Series 进行更改;请改用‘index’或‘labels’。

新增于版本 3.4.0。

level int or level name, optional

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

inplace: bool, default False

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

新增于版本 3.4.0。

Returns
Series

移除了指定索引标签的序列。

另请参阅

Series.dropna

示例

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

删除单个标签 A

>>> s.drop('A')
B    1
C    2
dtype: int64

删除标签 B 和 C

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

使用‘index’而不是‘labels’返回完全相同的结果。

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

对于‘columns’,不会对Series进行任何更改。

>>> s.drop(columns=['A'])
A    0
B    1
C    2
dtype: int64

使用‘inplace=True’,在原地进行操作并返回None。

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

也支持MultiIndex

>>> midx = pd.MultiIndex([['lama', 'cow', 'falcon'],
...                       ['speed', 'weight', 'length']],
...                      [[0, 0, 0, 1, 1, 1, 2, 2, 2],
...                       [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> s = ps.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
>>> s.drop(('lama', 'weight'))
lama    speed      45.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([('lama', 'speed'), ('falcon', 'weight')])
lama    weight    200.0
        length      1.2
cow     speed      30.0
        weight    250.0
        length      1.5
falcon  speed     320.0
        length      0.3
dtype: float64