How to divide a pandas series with scalar using series.div() method?

PandasServer Side ProgrammingProgramming

In the pandas series constructor, the div() or divide() method is used to perform element-wise floating division operation between the two series objects or between a series and a scalar.

The method returns a series with resultant floating division values. We can also do the division operation between series and scalar.

Here we will see some examples for dividing a series with a scalar.

Example 1

import pandas as pd

# create pandas Series
series = pd.Series([13,48,6,72,8])

print("Series object:",series)

# divide
print("combined series:",series.div(2))

Explanation

In this example, we will divide the Series with a scalar value “2”. Initially, we have created a pandas series object using a list of integer values. After that, we performed the division operation.

Output

Series object:
0 13
1 48
2  6
3 72
4  8
dtype: int64

combined series:
0  6.5
1 24.0
2  3.0
3 36.0
4  4.0
dtype: float64

In the above block, we can see both initial and resultant series objects. The element-wise division operation is performed between series elements and scaler value “2”.

Example 2

import pandas as pd

# create pandas Series
series = pd.Series([13,48,6,72,8])

print("Series object:",series)

# divide
print("combined series:",series.div([1,2,3,4,5]))

Explanation

Initially, we have created a pandas Series using a python list of integer values. And applied the div() method with a list of scalar values.

Output

Series object:
0 13
1 48
2  6
3 72
4  8
dtype: int64

combined series:
0 13.0
1 24.0
2  2.0
3 18.0
4  1.6
dtype: float64

The Series.div() method returns a series with floating division values.

raja
Updated on 09-Mar-2022 09:32:08

Advertisements