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


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

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

Pandas Series.truediv()函數執行係列和其他按元素的浮點數除法(二進製運算符truediv)。相當於series / other,但支持用fill_value代替輸入之一中的丟失數據。

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

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

返回:結果:係列

範例1:采用Series.truediv()函數執行給定係列對象與其他標量的浮點除法。

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

輸出:

現在我們將使用Series.truediv()函數執行給定係列對象與其他標量的浮點除法。

# divide all elements of the series by 10 
sr.truediv(other = 10)

輸出:

正如我們在輸出中看到的,Series.truediv()函數已成功執行給定Series對象的按元素劃分。

範例1:采用Series.truediv()函數執行給定係列對象與其他標量的浮點除法。給定的Series對象包含一些缺失值。處理缺少的值。


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

輸出:

現在我們將使用Series.truediv()函數執行給定係列對象與其他標量的浮點除法。用100填充缺失值。

# divide all elements of the series by 10 
# Also fill all the missing places by 100 
sr.truediv(other = 10, fill_value = 100)

輸出:

正如我們在輸出中看到的,Series.truediv()函數已成功執行給定Series對象的按元素劃分。在執行除法運算之前,它還在缺失值的位置替換了100。



相關用法


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