Pandas 係列是帶有軸標簽的一維ndarray。標簽不必是唯一的,但必須是可哈希的類型。該對象同時支持基於整數和基於標簽的索引,並提供了許多方法來執行涉及索引的操作。
Pandas Series.drop()
函數返回除去指定索引標簽的Series。它根據指定的索引標簽刪除係列的元素。
用法: Series.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=’raise’)
參數:
labels:索引標簽下降。
axis:冗餘適用於係列。
index, columns:冗餘,適用於Series,但可以使用索引代替標簽。
level:對於MultiIndex,將為其刪除標簽的級別。
inplace:如果為True,則執行就地操作並返回None。
errors:如果為“忽略”,則抑製錯誤並僅刪除現有標簽。
返回:掉落: Pandas 係列
範例1:采用Series.drop()
函數刪除與給定係列對象中傳遞的索引標簽相對應的值。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([80, 25, 3, 25, 24, 6])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the index
sr.index = index_
# Print the series
print(sr)
輸出:
現在我們將使用Series.drop()
函數刪除與給定係列對象中傳遞的索引標簽相對應的值。
# drop the passed labels
result = sr.drop(labels = ['Sprite', 'Dew'])
# Print the result
print(result)
輸出:
正如我們在輸出中看到的,Series.drop()
函數已成功刪除與傳遞的索引標簽相對應的條目。
範例2:采用Series.drop()
函數刪除與給定係列對象中傳遞的索引標簽相對應的值。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([11, 11, 8, 18, 65, 18, 32, 10, 5, 32, 32])
# Create the Index
index_ = pd.date_range('2010-10-09', periods = 11, freq ='M')
# set the index
sr.index = index_
# Print the series
print(sr)
輸出:
現在我們將使用Series.drop()
函數刪除與給定係列對象中傳遞的索引標簽相對應的值。
# drop the passed labels
result = sr.drop(labels = [pd.Timestamp('2010-12-31'),
pd.Timestamp('2011-04-30'), pd.Timestamp('2011-08-31')])
# Print the result
print(result)
輸出:
正如我們在輸出中看到的,Series.drop()
函數已成功刪除與傳遞的索引標簽相對應的條目。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Series.drop()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。