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


Python Pandas Series.rsub()用法及代碼示例

Pandas 係列是帶有軸標簽的一維ndarray。標簽不必是唯一的,但必須是可哈希的類型。該對象同時支持基於整數和基於標簽的索引,並提供了許多方法來執行涉及索引的操作。

Pandas Series.rsub()函數返回係列和其他逐元素的減法(二進製運算符rsub)。相當於other - series,但支持用fill_value代替輸入之一中的丟失數據。

用法: Series.rsub(other, level=None, fill_value=None, axis=0)

參數:
other:序列或標量值
fill_value:填寫現有的缺失(NaN)值
level:在一個級別上廣播,在傳遞的MultiIndex級別上匹配索引值

返回:結果:係列

範例1:采用Series.rsub()函數,以標量元素為單位執行給定Series對象的反向減法。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series([100, 25, 32, 118, 24, 65]) 
  
# Create the Index 
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp'] 
  
# set the index 
sr.index = index_ 
  
# Print the series 
print(sr)

輸出:

現在我們將使用Series.rsub()函數使用標量對給定Series對象執行逐元素的逆減法。

# perform reverse subtraction with 1000 
selected_items = sr.rsub(other = 1000) 
  
# Print the returned Series object 
print(selected_items)

輸出:

正如我們在輸出中看到的,Series.rsub()函數已成功返回標量與給定Series對象的反減。

範例2:采用Series.rsub()函數,以標量元素為單位執行給定Series對象的反向減法。給定的Series對象還包含一些缺少的值。


# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series([19.5, 16.8, None, 22.78, None, 20.124, None, 18.1002, None]) 
  
# Print the series 
print(sr)

輸出:

現在我們將使用Series.rsub()函數使用標量對給定Series對象執行逐元素的逆減法。我們還將在給定Series對象中所有缺失值的位置替換100。

# perform reverse subtraction with 1000 
# fill 100 at the place of all missing values 
selected_items = sr.rsub(other = 1000, fill_value = 100) 
  
# Print the returned Series object 
print(selected_items)

輸出:

正如我們在輸出中看到的,Series.rsub()函數已成功返回標量與給定Series對象的反減。



相關用法


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