What is the use of rfloordiv() function in pandas series?


The series.rfloordiv() function is used to apply the integer division operation to a pandas series objective by others, and it performs an element-wise division operation. The method rfloordiv is called reverse floordiv and is similar to the floordiv() method, but instead of calculating series // other it calculates other // series.

The method supports the substitution of missing values in any of the inputs by using one of its parameter called fill_value. And the method has two more parameters called other, and level. Another is a 2nd input object (series or scalar), and the level is broadcast across a level.

Example 1

In this following example, we will apply the reverse floor divide operation to the Series object by a scalar value “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))

Output

The output is given below −

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

In the above block, we can see both initial and resultant series objects. And the second one is the result of element-wise reverse integer division operation between series and a scalar value “100”.

Example 2

In the following example, we will perform a reverse integer division operation between two series objects by using the rfloordiv() method.

# 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))

Output

The output is given below −

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

In the above output block, we can see the output of reverse floor division operation. There are 2 Nan elements present in the resultant series object which are present because the value at index position “R” is not available in the other series object “series2”, as well as index “F” is not available in the called series object “series1”. Due to this, those two values are filled by default value Nan.

Updated on: 07-Mar-2022

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements