Signup/Sign In
PUBLISHED ON: MARCH 30, 2021

Pandas DataFrame rdiv() Method

In this tutorial, we will learn the Python pandas DataFrame.rdiv() method. It returns a floating division of dataframe and other, element-wise. It returns a DataFrame with the result of the arithmetic operation. It is the reverse version of the DataFrame.div() method.

The below is the syntax of the python pandas DataFrame.rdiv() method.

Syntax

DataFrame.rdiv(other, axis='columns', level=None, fill_value=None)

Parameters

other: It can be a scalar, sequence, Series, or DataFrame. Any single or multiple element data structure, or list-like object.

axis: It represents index or column axis, '0' or 'index' for index and '1' or 'columns' for the column axis. When the axis=0 or index, method applied over the index axis and when the axis=1 or columns method applied over the column axis. For Series input, axis to match Series index on.

level: It represents the int or label. It broadcasts across a level, matching Index values on the passed MultiIndex level.

fill_value: float or None, default None. Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Example 1: Divide DataFrame by using DataFrame.rdiv() Method

The below example shows the dividing of DataFrame by constant using the DataFrame.rdiv() method. For a/b, a is the scalar and b is the DataFrame elements. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df= pd.DataFrame({'a': [1,6,2],'b': [3,4,6],'c': [12,1,0]})
print("--------The DataFrame is----------")
print(df)
print("---------------------------------")
print(df.rdiv(12))


--------The DataFrame is----------
a b c
0 1 3 12
1 6 4 1
2 2 6 0
---------------------------------
a b c
0 12.0 4.0 1.0
1 2.0 3.0 12.0
2 6.0 2.0 inf

Example 2: Divide DataFrame by using DataFrame.div() Method

The below example shows the dividing of DataFrame with other dataframe using the DataFrame.div() method. For a/b, a is the df2(other DataFrame) and b is the df1 elements. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrames
df1 = pd.DataFrame({'a': [2, 2, 2],'b': [2, 2, 2],'c': [2, 2, 2]})
df2 = pd.DataFrame({'a': [2, 5, 6],'b': [8, 10, 12],'c': [14, 16, 18]})
print("---------------------------------")
print(df1.rdiv(df2))


---------------------------------
a b c
0 1.0 4.0 7.0
1 2.5 5.0 8.0
2 3.0 6.0 9.0

Example 3: Divide DataFrame by using the DataFrame.rdiv() Method and filling null values

The below example shows the dividing of DataFrame with other using the DataFrame.rdiv() method with fill_value=.See the below example. Here the two DataFrames are not aligned and the DataFrame.rdiv() does not raise error instead it gives the Nan values and fill those Nan values using the fill_value parameter.

import pandas as pd
df1 = pd.DataFrame({'a': [2, 5, 6],'b': [8, 10, 12]})
df2 = pd.DataFrame({'a': [2, 2, 2],'b': [2, 2, 2],'c': [2, 2, 2]})
print(df1.rdiv(df2,fill_value=2))


---------------------------------
a b c
0 1.0 4.0 7.0
1 2.5 5.0 8.0
2 3.0 6.0 9.0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.rdiv() method. We learned syntax, parameters, and solved examples by applying this method on the DataFrame and understood the method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.