Pandas DataFrame.rmod


Pandas DataFrame.rmod

The DataFrame.rmod method in pandas performs the reverse modulo operation. It calculates the remainder of division, where the DataFrame's elements are the divisors, and the elements in other (scalar, Series, or DataFrame) are the dividends. This method is particularly useful for element-wise operations with custom modulo behavior.


Syntax

The syntax for DataFrame.rmod is:

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

Here, DataFrame refers to the pandas DataFrame performing the reverse modulo operation.


Parameters

ParameterDescription
otherScalar, Series, or DataFrame to perform the operation with. Represents the dividends in the modulo operation.
axisDetermines the alignment axis for the operation. 'index' (0) aligns with rows, and 'columns' (1) aligns with columns. Defaults to 'columns'.
levelFor MultiIndex, specifies the level to align with. Defaults to None.
fill_valueValue to substitute for missing data (NaN) in the DataFrame or other. Defaults to None.

Returns

A DataFrame resulting from the element-wise reverse modulo operation.


Examples

Basic Reverse Modulo with a Scalar

Perform a reverse modulo operation where each element of the DataFrame is the divisor, and the scalar value is the dividend.

Python Program

import pandas as pd

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

# Perform reverse modulo operation with a scalar
result = df.rmod(10)
print("Reverse Modulo with a Scalar:")
print(result)

Output

Reverse Modulo with a Scalar:
   A  B
0  0  1
1  0  4
2  2  1

Reverse Modulo with Another DataFrame

Perform a reverse modulo operation between two DataFrames.

Python Program

import pandas as pd

# Create two DataFrames
data1 = {
    'A': [2, 5, 8],
    'B': [3, 6, 9]
}
data2 = {
    'A': [10, 15, 20],
    'B': [30, 25, 40]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Perform reverse modulo operation
result = df1.rmod(df2)
print("Reverse Modulo with Another DataFrame:")
print(result)

Output

Reverse Modulo with Another DataFrame:
   A  B
0  0  0
1  0  1
2  4  4

Handling Missing Data with fill_value

Use fill_value to replace missing values in the DataFrame or other before performing the operation.

Python Program

import pandas as pd
import numpy as np

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

# Perform reverse modulo with a scalar, filling missing values
result = df.rmod(10, fill_value=1)
print("Reverse Modulo with fill_value:")
print(result)

Output

Reverse Modulo with fill_value:
     A    B
0  0.0  0.0
1  0.0  4.0
2  2.0  1.0

Reverse Modulo with a Series

Perform a reverse modulo operation between a DataFrame and a Series, aligning the operation along a specific axis.

Python Program

import pandas as pd

# Create a DataFrame and a Series
data = {
    'A': [2, 5, 8],
    'B': [3, 6, 9]
}
df = pd.DataFrame(data)
series = pd.Series([10, 20, 30])

# Perform reverse modulo along index
result = df.rmod(series, axis='index')
print("Reverse Modulo with a Series:")
print(result)

Output

Reverse Modulo with a Series:
    A   B
0   0   1
1   0   2
2   6   3

Summary

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

  • Using rmod to calculate reverse modulo element-wise with scalars, Series, or DataFrames.
  • Handling missing data using the fill_value parameter.
  • Controlling alignment with the axis parameter.

The DataFrame.rmod method is a flexible tool for performing customized modulo operations in pandas DataFrames.


Python Libraries