Applying floor division a.k.a integer division to a pandas series by another series

Overview:

  • The operation Floor Division is also called as Integer Division. It is a binary operation that takes two values and generates a new value.
  • In Integer Division, the fractional remainder part is discarded and the quotient, an integer (not a fraction) is returned.
  • The floordiv() function of the pandas Series class, does an integer division for each element of a Series by the corresponding element of an another Series.

 

Example - Integer division of one pandas Series by another Series:

# Example Python program to apply integer division on a pandas Series by another Series

import pandas as pds

 

consignmentSize = [1000, 2000, 500, 3000, 750];

shopCount       = [5, 4, 10, 7, 9];

 

series1 = pds.Series(consignmentSize);

series2 = pds.Series(shopCount);

 

# Divide pandas series1 by series2

itemsPerShop = series1.floordiv(series2);

print("Series1:");

print(series1);

 

print("Series2:");

print(series2);

 

print("Result of applying floor division to Series1 by Series2:");

print(itemsPerShop);

 

 

Output:

Series1:

0    1000

1    2000

2     500

3    3000

4     750

dtype: int64

Series2:

0     5

1     4

2    10

3     7

4     9

dtype: int64

Result of applying floor division to Series1 by Series2:

0    200

1    500

2     50

3    428

4     83

dtype: int64

 

Example-Integer division of a pandas Series by a Python list:

Integer division can also be applied to a pandas Series by another Python sequence like a list or a tuple. The Python example below applies integer division to the elements of a pandas Series by the elements of a Python list.

# Example Python program to divide a pandas Series by another Series

import pandas as pds

 

# List of hexagonal numbers

hexagonalNumbers = [1, 6, 15, 28, 45];

 

# List of prime numbers

primeNumbers     = [1, 2, 3, 5, 7];

 

# Load the Python list that contains the hexagonal numbers into a pandas Series

series  = pds.Series(hexagonalNumbers);

 

# Floor division of Series1 by Series2

result = series.floordiv(primeNumbers);

 

print("Contents of pandas Series:");

print(series);

 

print("Contents of Python List:");

print(primeNumbers);

 

print("Result of a pandas Series of integers floor divided by a Python List of integers:");

print(result);

 

Output:

Contents of pandas Series:

0     1

1     6

2    15

3    28

4    45

dtype: int64

Contents of Python List:

[1, 2, 3, 5, 7]

Result of a pandas Series of integers floor divided by a Python List of integers:

0    1

1    3

2    5

3    5

4    6

dtype: int64


Copyright 2024 © pythontic.com