Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas dataframe.rtruediv() function is used for finding the floating point division of dataframe and other, element-wise (binary operator rfloordiv). This function is essentially same as doing other / dataframe but with a support to substitute for missing data in one of the inputs.Â
Syntax:DataFrame.rtruediv(other, axis=’columns’, level=None, fill_value=None)Â
Parameters :Â
other : Series, DataFrame, or constantÂ
axis : For Series input, axis to match Series index onÂ
level : Broadcast across a level, matching Index values on the passed MultiIndex level
fill_value : 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.
Returns : result : DataFrame
Â
Example #1: Use rtruediv() function to divide each element of a series to a corresponding value in a dataframe over the column axis.Â
Python3
# importing pandas as pdimport pandas as pdÂ
# Creating the dataframe df = pd.DataFrame({"A":[1, 5, 3, 4, 2],                   "B":[3, 2, 4, 3, 4],                   "C":[2, 2, 7, 3, 4],                    "D":[4, 3, 6, 12, 7]},                    index =["A1", "A2", "A3", "A4", "A5"])Â
# Print the dataframedf |
Let’s create the seriesÂ
Python3
# importing pandas as pdimport pandas as pdÂ
# Create the seriessr = pd.Series([12, 25, 64, 18], index =["A", "B", "C", "D"])Â
# Print the seriessr |
Lets use the dataframe.rtruediv() function to divide each element in a series with the corresponding element in the dataframe.
Python3
# equivalent to sr / dfdf.rtruediv(sr, axis = 1) |
Output :Â
Example #2: Use rtruediv() function to divide each element in a dataframe with the corresponding element in other dataframe
Python3
# importing pandas as pdimport pandas as pdÂ
# Creating the first dataframe df1 = pd.DataFrame({"A":[1, 5, 3, 4, 2],                    "B":[3, 2, 4, 3, 4],                     "C":[2, 2, 7, 3, 4],                    "D":[4, 3, 6, 12, 7]},                      index =["A1", "A2", "A3", "A4", "A5"])Â
# Creating the second dataframedf2 = pd.DataFrame({"A":[10, 11, 7, 8, 5],                    "B":[21, 5, 32, 4, 6],                     "C":[11, 21, 23, 7, 9],                     "D":[1, 5, 3, 8, 6]},                      index =["A1", "A2", "A3", "A4", "A5"])Â
# Print the first dataframeprint(df1)Â
# Print the second dataframeprint(df2) |
Lets perform df2 / df1Â
Python3
# divide df2 by df1df1.rtruediv(df2) |
Output :Â
Â

