Modulo Division of a Dataframe Using Python Pandas

The mod() function of a DataFrame class returns the remainder of an integer division operation. The mod() does the modulo division in an element-wise fashion to a DataFrame by another DataFrame.

Dataframe Binaryoperatorfunctions Updated 19 Jul 2019 1 min read

Overview

  • The mod() method is one of the several binary operations provided by the DataFrame class of Python pandas library.
  • The mod() method does a modulo division of an element from a DataFrame using another DataFrame.
  • While the floordiv() method of DataFrame returns the quotient from the integer division, the mod() method returns the remainder of the integer division.

Example

python
import pandas as pd

# Data for DataFrame instances

m1 = [(3, 6, 7),

      (13, 14, 17),

      (21, 24, 25)];

m2 = [(3, 2, 3),

      (4, 0, 16),

      (7, 0, 9)];

# DataFrames

dataFrame1 = pd.DataFrame(data=m1);

dataFrame2 = pd.DataFrame(data=m2);

# Do a modulo division

resultantDataFrame = dataFrame1.mod(dataFrame2);

print("DataFrame1 modulo divided by DataFrame2:")

print(resultantDataFrame);

Output

Output
DataFrame1 modulo divided by DataFrame2:

     0    1    2

0  0.0  0.0  1.0

1  1.0  NaN  1.0

2  0.0  NaN  7.0

##