Signup/Sign In

Pandas DataFrame mod() Method

In this tutorial, we will learn the Python pandas DataFrame.mod() method. This method returns the Modulo of the dataframe and other, element-wise. It returns a DataFrame with the result of the arithmetic operation.

The below is the syntax of the python pandas DataFrame.mod() method.

Syntax

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

Parameters

other: It represents the scalar, sequence, Series, or DataFrame. Any single or multiple element data structure, or list-like object.

axis: '0' represents the index and '1' represents the columns. When the axis=0, method applied over the index axis and when the axis=1 method applied over the column axis. For the input Series , axis to match Series index on.

level: It represents an int or label. It broadcasts across a level, matching index values on the passed MultiIndex level.

fill_value: It represents the float or None, the default value is None. It fills the existing missing or null values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Example 1: Find modulo by using the DataFrame.mod() Method

The below example shows the getting the modulo of the of DataFrame by a constant value using the DataFrame.mod() method.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df1 = pd.DataFrame({'a': [2, 5, 6],'b': [8, 10, 12],'c': [14, 16, 18]})
print(df1.mod(3))

Once we run the program we will get the following output.


a b c
0 2 2 2
1 2 1 1
2 0 0 0

Example 2: Find modulo by using the DataFrame.mod() Method

The below example shows the getting the modulo of the of DataFrame with other dataframe values using the DataFrame.mod() method.

import pandas as pd
df1 = pd.DataFrame({'a': [2, 5, 6],'b': [8, 10, 12],'c': [14, 16, 18]})
df2 = pd.DataFrame({'a': [2, 2, 2],'b': [2, 2, 2],'c': [2, 2, 2]})
print(df1.mod(df2))

Once we run the program we will get the following output.


a b c
0 0 0 0
1 1 0 0
2 0 0 0

Example 3: Find modulo by using the DataFrame.mod() Method

This example shows the getting the modulo of the DataFrame with the other dataframe using the DataFrame.mod() method with fill_value=1. It will add default values in the missing places and replaces the null values.

import pandas as pd
df1 = pd.DataFrame({'a': [2, 5, 6],'b': [8, 10, 12],'c': [14, 16, 18]})
df2 = pd.DataFrame({'a': [2, 2, 2],'b': [2, 2, 2]})
print(df1.mod(df2,fill_value=1))

Once we run the program we will get the following output.


a b c
0 0 0 0.0
1 1 0 0.0
2 0 0 0.0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.mod() method. We learned the syntax, parameters of this method and applied them to the DataFrame to understood the method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.