pandas 系列中 rfloordiv() 函数有什么用?

阅读 138 收藏 0 点赞 0 评论 0

该函数用于将整数除法运算应用于其他人的熊猫系列目标,并执行逐元素除法运算。rfloordiv 方法称为反向 floordiv,与方法类似,但不是计算系列 // other 而是计算其他 // 系列。series.rfloordiv()floordiv()

该方法支持通过使用其名为 fill_value 的参数之一来替换任何输入中的缺失值。该方法还有两个参数,分别称为 other 和 level。另一个是第二个输入对象(系列或标量),并且该级别在一个级别上广播。

示例 1

在以下示例中,我们将按标量值“100”对 Series 对象应用反向除法运算。

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

在上面的输出块中,我们可以看到反向楼层除法运算的输出。结果系列对象中存在 2 个 Nan 元素,这是因为索引位置“R”处的值在其他系列对象“series2”中不可用,以及索引“F”在调用的系列对象中不可用“系列1”。因此,这两个值由默认值 Nan 填充。

评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号