當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python pandas.Series.drop用法及代碼示例

用法:

Series.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

返回刪除指定索引標簽的係列。

根據指定的索引標簽刪除係列的元素。使用multi-index 時,可以通過指定級別來移除不同級別的標簽。

參數

labels單標簽或list-like

要刪除的索引標簽。

axis0,默認 0

為係列上的應用程序提供冗餘。

index單標簽或list-like

Series 上的應用程序冗餘,但可以使用 ‘index’ 代替 ‘labels’。

columns單標簽或list-like

係列沒有變化;請改用‘index’ or ‘labels’。

levelint 或級別名稱,可選

對於 MultiIndex,將刪除標簽的級別。

inplace布爾值,默認為 False

如果為 True,則在原地執行操作並返回 None。

errors{‘ignore’, ‘raise’},默認 ‘raise’

如果‘ignore’,抑製錯誤並且僅刪除現有標簽。

返回

係列或無

刪除了指定索引標簽的係列,如果 inplace=True 則為無。

拋出

KeyError

如果在索引中找不到任何標簽。

例子

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

在 MultiIndex 係列中刪除第二級標簽

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

相關用法


注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.Series.drop。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。