Pandas序列中rfloordiv()函式的作用是什麼?


series.rfloordiv()函式用於對Pandas序列物件應用整數除法運算,它執行逐元素的除法運算。rfloordiv方法稱為反向向下取整除法,類似於floordiv()方法,但它計算的是other // series而不是series // other。

此方法支援使用名為fill_value的引數替換任何輸入中的缺失值。該方法還有另外兩個引數:other和level。other是第二個輸入物件(序列或標量),level則在某個級別上進行廣播。

示例1

在下面的示例中,我們將對序列物件應用反向向下取整除法運算,除數為標量值“100”。

import pandas as pd

# create pandas Series
series = pd.Series([36, 7, 45, 39, 2])

print("Series object:",series)

# Apply reverse floor division method with a scalar value
print("Output:")
print(series.rfloordiv(100))

輸出

輸出如下:

Series object:
0    36
1    7
2    45
3    39
4    2
dtype: int64

Output:
0    2
1    14
2    2
3    2
4    50
dtype: int64

在上面的程式碼塊中,我們可以看到初始序列物件和結果序列物件。第二個物件是序列和標量值“100”之間逐元素反向整數除法運算的結果。

示例2

在下面的示例中,我們將使用rfloordiv()方法對兩個序列物件執行反向整數除法運算。

# import pandas packages
import pandas as pd

# Creating Pandas Series objects
series1 = pd.Series([1, 26, 36, 38], index=list("PQRS"))
print('First series object:',series1)

series2 = pd.Series([12, 74, 72, 61], index=list("PQST"))
print('Second series object:',series2)

# apply reverse floor division method
print("Reverse floor division of Series1 and Series2:", series1.rfloordiv(series2))

輸出

輸出如下:

First series object:
P    1
Q    26
R    36
S    38
dtype: int64

Second series object:
P    12
Q    74
S    72
T    61
dtype: int64

Reverse floor division of Series1 and Series2:
P    12.0
Q    2.0
R    NaN
S    1.0
T    NaN
dtype: float64

在上面的輸出程式碼塊中,我們可以看到反向向下取整除法運算的結果。結果序列物件中有兩個NaN元素,這是因為在另一個序列物件“series2”中找不到索引位置“R”的值,而在被呼叫序列物件“series1”中找不到索引“F”的值。因此,這兩個值預設為NaN。

更新於:2022年3月7日

128次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.