Pandas DataFrame.rtruediv


Pandas DataFrame.rtruediv

The DataFrame.rtruediv method in pandas performs the reverse division operation (other / DataFrame) element-wise. It allows dividing a scalar, Series, or another DataFrame by the elements of the DataFrame while handling missing data and supporting optional parameters for alignment and filling missing values.


Syntax

The syntax for DataFrame.rtruediv is:

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

Here, DataFrame refers to the pandas DataFrame being used for the reverse division operation.


Parameters

ParameterDescription
otherA scalar, Series, or DataFrame to divide by the DataFrame elements.
axisDefines the axis along which the operation is performed. Default is 'columns' (axis=1). Use 'index' (axis=0) to match rows.
levelIf the axis is a MultiIndex (hierarchical), this parameter specifies the level to align with.
fill_valueValue to replace missing data in the DataFrame or other before the operation. Default is None.

Returns

A DataFrame with the result of the reverse division operation.


Examples

Reverse Division with a Scalar

Divide a scalar by the elements of a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6]
}
df = pd.DataFrame(data)

# Perform reverse division with a scalar
result = 10 / df
print("Reverse Division with a Scalar:")
print(result)

Output

Reverse Division with a Scalar:
          A         B
0  10.000000  2.500000
1   5.000000  2.000000
2   3.333333  1.666667

Reverse Division with a Series

Divide a Series by the elements of a DataFrame along rows.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6]
}
df = pd.DataFrame(data)

# Create a Series
series = pd.Series([10, 20, 30])

# Perform reverse division with a Series
result = series.rtruediv(df, axis=0)
print("Reverse Division with a Series:")
print(result)

Output

Reverse Division with a Series:
          A         B
0  10.000000  2.500000
1  10.000000  4.000000
2  10.000000  5.000000

Reverse Division with Another DataFrame

Divide the elements of one DataFrame by another DataFrame.

Python Program

import pandas as pd

# Create two DataFrames
data1 = {
    'A': [1, 2, 3],
    'B': [4, 5, 6]
}
data2 = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Perform reverse division with another DataFrame
result = df2.rtruediv(df1)
print("Reverse Division with Another DataFrame:")
print(result)

Output

Reverse Division with Another DataFrame:
          A         B
0  0.100000  0.100000
1  0.100000  0.100000
2  0.100000  0.100000

Handling Missing Data with fill_value

Replace missing data before performing the operation using fill_value.

Python Program

import pandas as pd
import numpy as np

# Create a DataFrame with missing values
data = {
    'A': [1, 2, np.nan],
    'B': [4, np.nan, 6]
}
df = pd.DataFrame(data)

# Perform reverse division with a scalar, using fill_value
result = df.rtruediv(10, fill_value=1)
print("Reverse Division with Missing Data:")
print(result)

Output

Reverse Division with Missing Data:
          A         B
0  10.000000  2.500000
1   5.000000  10.000000
2  10.000000  1.666667

Summary

In this tutorial, we explored the DataFrame.rtruediv method in pandas. Key takeaways include:

  • Using reverse division to compute other / DataFrame element-wise.
  • Handling missing data with the fill_value parameter.
  • Applying reverse division with scalars, Series, or other DataFrames.

The DataFrame.rtruediv method is a flexible and powerful tool for performing element-wise division operations in pandas.


Python Libraries