Pandas DataFrame.rdiv


Pandas DataFrame.rdiv

The DataFrame.rdiv method in pandas is used for reverse division. It performs element-wise division where the DataFrame is the divisor, and another object (scalar, Series, or DataFrame) is the dividend. This method is useful for performing arithmetic operations where the order of operands is reversed.


Syntax

The syntax for DataFrame.rdiv is:

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

Here, DataFrame is the divisor, and other is the dividend.


Parameters

ParameterDescription
otherScalar, Series, or DataFrame to divide by the DataFrame.
axisAxis to match when performing the operation. Default is 'columns'.
levelIf the axis is a MultiIndex, this parameter specifies the level to align on. Default is None.
fill_valueValue to fill in missing values for either the DataFrame or other. Default is None.

Returns

A pandas DataFrame with the reverse division operation applied element-wise.


Examples

Reverse Division with a Scalar

Perform element-wise reverse division where the DataFrame is divided into a scalar value.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
df = pd.DataFrame(data)

# Perform reverse division with a scalar
result = df.rdiv(100)
print("Reverse division with scalar:")
print(result)

Output

Reverse division with scalar:
      A         B
0  10.0  2.500000
1   5.0  2.000000
2   3.333333  1.666667

Reverse Division with Another DataFrame

Perform element-wise reverse division between two DataFrames.

Python Program

import pandas as pd

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

# Perform reverse division
result = df1.rdiv(df2)
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

Reverse Division with a Series

Perform reverse division with a Series, aligning the operation along the columns by default.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
df = pd.DataFrame(data)

# Create a Series
series = pd.Series([2, 5])

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

Output

Reverse division with a Series:
      A    B
0  0.2  0.05
1  0.5  0.10

Reverse Division with Missing Values

Use the fill_value parameter to handle missing values during the operation.

Python Program

import pandas as pd
import numpy as np

# Create a DataFrame with NaN values
data = {
    'A': [10, 20, np.nan],
    'B': [40, np.nan, 60]
}
df = pd.DataFrame(data)

# Perform reverse division with a scalar, using fill_value
result = df.rdiv(100, fill_value=1)
print("Reverse division with missing values handled:")
print(result)

Output

Reverse division with missing values handled:
           A          B
0  10.000000   2.500000
1   5.000000  100.000000
2  100.000000   1.666667

Summary

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

  • Using rdiv to perform reverse division operations.
  • Handling operations with scalars, Series, or other DataFrames.
  • Dealing with missing values using the fill_value parameter.

The DataFrame.rdiv method is a versatile tool for element-wise reverse arithmetic operations in pandas.


Python Libraries