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.
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
Your privacy choices
We'd like to use Google Analytics cookies to count visits and see which tutorials are useful.
They are only set if you accept. We don't use advertising cookies and we don't sell your data.
You can change your choice any time from “Your privacy choices” at the bottom of every page.
Privacy Policy
Your privacy choices
Your browser sent a Global Privacy Control signal, so analytics is off unless you turn it on here.
Strictly necessary
Remembers this choice in your browser. No cookies, nothing sent to anyone.Always on