Modulo Division Of A Dataframe Using Python Pandas

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:

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:

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

 

 


Copyright 2023 © pythontic.com