Dividing a pandas Series by another Series

Division is a common binary operation (Dyadic operation) in Data analysis. Elements of one pandas Series can be divided by elements of another pandas series through the Series.div() function as shown in the Python example code.

Series Binaryoperatorfunctions Updated 26 Aug 2019 2 min read

Overview

  • Division is a common binary operation in Data Analysis.
  • A binary operation works on two operands and returns a new value.
  • The function div() of Series class divides the elements present in a pandas Series, by the elements present in another Series.
  • In the same way to divide a pandas DataFrame by another DataFrame, the method DataFrame.div()can be used.

Dividing the elements of a Pandas Series by the elements of another Series

  • The following example has sum values present in a pandas.Series object and period values present in another pandas.Series object.
  • To obtain the mean or average values, the first series divided by the second series through the div() function.
  • The values present in the resultant series are rounded to two digits after decimal point, using the Series.round() function.

Example

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

import pandas as pds

sum     = [750, 1350, 2400, 3000];

period  = [7, 14, 21, 28];

series1 = pds.Series(sum);

series2 = pds.Series(period);

# Divide pandas series1 by series2

average = series1.div(series2);

print("The sum series:");

print(series1);

print("The period series:");

print(series2);

print("The mean or average series (Values rounded to two decimals):");

print(average.round(2));

Output

Output
The sum series:

0     750

1    1350

2    2400

3    3000

dtype: int64

The period series:

0     7

1    14

2    21

3    28

dtype: int64

The mean or average series (Values rounded to two decimals):

0    107.14

1     96.43

2    114.29

3    107.14

dtype: float64

Example - Dividing the elements of a pandas Series by the elements of a Python list

python
# Example Python program to divide a pandas Series by a Python Sequence

import pandas as pds

# Series to be divided - a Catalan Series

catalanSeries = pds.Series([1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]);

# Divisor - a Fibonacci Series contained in a Python list

divisor       = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55];

# Divide the elements of a pandas series by the elements of a Python list

result        = catalanSeries.div(divisor)

print("Contents of the pandas Series:");

print(catalanSeries);

print("Contents of the Python list:");

print(divisor);

print("Resultant pandas Series after dividing the series elements by the list elements:");

print(result.round(2));

Output

Output
Contents of the pandas Series:

0       1

1       1

2       2

3       5

4      14

5      42

6     132

7     429

8    1430

9    4862

dtype: int64

Contents of the Python list:

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Resultant pandas Series after dividing the series elements by the list elements:

0     1.00

1     1.00

2     1.00

3     1.67

4     2.80

5     5.25

6    10.15

7    20.43

8    42.06

9    88.40

dtype: float64