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
| Parameter | Description |
|---|---|
other | Can be a scalar, Series, or DataFrame. The object to be used for the modulo operation. |
axis | Axis along which the operation is performed. 'columns' (default) applies the operation across columns, and 'index' applies it across rows. |
level | Broadcast across a specified level (useful for MultiIndex). Default is None. |
fill_value | Value 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 4Modulo 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 0Modulo 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 0Handling 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 4Summary
In this tutorial, we explored the DataFrame.mod method in pandas. Key takeaways include:
- Using
modfor element-wise modulo operations with scalars, Series, or DataFrames. - Handling missing values using the
fill_valueparameter. - Customizing the operation with the
axisparameter.
The DataFrame.mod method is a powerful tool for performing modulo operations on pandas DataFrames.