Pandas DataFrame.mod


Pandas DataFrame.mod

The DataFrame.mod method in pandas is used to compute the modulo (remainder) of one DataFrame (or Series) with another DataFrame, Series, or scalar value. It performs element-wise modulo operations.


Syntax

The syntax for DataFrame.mod is:

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

Here, DataFrame refers to the pandas DataFrame on which the modulo operation is performed.


Parameters

ParameterDescription
otherCan be a scalar, Series, or DataFrame. The object to be used for the modulo operation.
axisAxis along which the operation is performed. 'columns' (default) applies the operation across columns, and 'index' applies it across rows.
levelBroadcast across a specified level (useful for MultiIndex). Default is None.
fill_valueValue to fill missing values in the DataFrame or other before performing the operation. Default is None.

Returns

A DataFrame containing the result of the element-wise modulo operation.


Examples

Modulo with a Scalar Value

Compute the modulo of all elements in the DataFrame with a scalar value.

Python Program

import pandas as pd

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

# Compute modulo with a scalar value
print("Modulo with scalar value 7:")
result = df.mod(7)
print(result)

Output

Modulo with scalar value 7:
   A  B  C
0  3  1  5
1  6  4  1
2  2  0  4

Modulo with Another DataFrame

Perform element-wise modulo between two DataFrames of the same shape.

Python Program

import pandas as pd

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

# Compute element-wise modulo
print("Element-wise modulo between DataFrames:")
result = df1.mod(df2)
print(result)

Output

Element-wise modulo between DataFrames:
   A  B  C
0  1  0  0
1  0  1  5
2  0  0  0

Modulo with a Series

Compute the modulo between a DataFrame and a Series, aligning the Series with the DataFrame’s columns.

Python Program

import pandas as pd

# Create a DataFrame and a Series
data = {
    'A': [10, 20, 30],
    'B': [15, 25, 35],
    'C': [40, 50, 60]
}
df = pd.DataFrame(data)
series = pd.Series({'A': 4, 'B': 5, 'C': 6})

# Compute modulo with Series
print("Modulo with Series:")
result = df.mod(series)
print(result)

Output

Modulo with Series:
   A  B  C
0  2  0  4
1  0  0  2
2  2  0  0

Handling Missing Values with fill_value

Use the fill_value parameter to handle 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': [10, 20, np.nan],
    'B': [15, np.nan, 35],
    'C': [40, 50, 60]
}
df = pd.DataFrame(data)

# Compute modulo with a scalar and handle missing values
print("Modulo with scalar value 7, filling missing values with 0:")
result = df.mod(7, fill_value=0)
print(result)

Output

Modulo with scalar value 7, filling missing values with 0:
     A    B  C
0  3.0  1.0  5
1  6.0  0.0  1
2  0.0  0.0  4

Summary

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

  • Using mod for element-wise modulo operations with scalars, Series, or DataFrames.
  • Handling missing values using the fill_value parameter.
  • Customizing the operation with the axis parameter.

The DataFrame.mod method is a powerful tool for performing modulo operations on pandas DataFrames.


Python Libraries