Integer division or floor division of a Dataframe using Python Pandas

Overview:

Unlike div(), the floordiv() method of the DataFrame class from Python pandas library, does an element-wise integer division. By integer division, it is meant that only the quotient, an integer is returned when one element of a DataFrame is divided by another element of a DataFrame.

 

Example:

import pandas as pd

 

# Data

m1 = [(3, 5, 7),

      (13, 15, 17),

      (21, 23, 25)];

 

m2 = [(1, 2, 3),

      (4, 5, 6),

      (7, 8, 9)];

              

# Data loaded into pandas DataFrames

dataFrame1 = pd.DataFrame(data=m1);

dataFrame2 = pd.DataFrame(data=m2);

 

# Do an integer division

resultantDataFrame = dataFrame1.floordiv(dataFrame2);         

 

print("DataFrame1 floor divided by DataFrame2:")

print(resultantDataFrame);

 

Output:

DataFrame1 floor divided by DataFrame2:

   0  1  2

0  3  2  2

1  3  3  2

2  3  2  2

 


Copyright 2024 © pythontic.com