How can we give the scalar value to the pandas series.divmod() method?

PandasServer Side ProgrammingProgramming

The Series.divmod() method in the pandas series constructor is used to perform both integer division and modular division operations on series objects with a scalar, or we can apply this divmod() method on two series also.

The method performs an element-wise division operation of its two input objects. And it returns a python tuple with two series objects, the first series of the tuple is representing the integer division output, and the second series object of the tuple representing the modulo division output.

Example 1

import pandas as pd

# create pandas Series
series = pd.Series([25, 48, 18, 99, 61])

print("Series object:",series)

# apply divmod()
print("Output: ",series.divmod([1,2,3,4,5]))

Explanation

In this example, we will divide a series object with a list of scalar values by using the divmod() method.

Output

Series object:
0 25
1 48
2 18
3 99
4 61
dtype: int64

Output:
(0 25
1  24
2   6
3  24
4  12
dtype: int64,

0 0
1 0
2 0
3 3
4 1
dtype: int64)

The divmod() method returns a tuple object with two series’s. The First series is representing the integer division values and the second one representing the output values of modular division.

Example 2

import pandas as pd

# create pandas Series
series = pd.Series([19, 4, 89, 45, 31])

print("Series object:",series)

# apply divmod() with a scalar value
print("Output: ",series.divmod(2))

Explanation

For the following example, we applied the divmod() method on a series object using a scalar value “2”.

Output

Series object:
0 19
1  4
2 89
3 45
4 31
dtype: int64

Output:
(0 9
1  2
2 44
3 22
4 15
dtype: int64,

0 1
1 0
2 1
3 1
4 1
dtype: int64)

In the above output block, we can see the output of the divmod() method which is a tuple with two series objects(integer division and modular division).

raja
Updated on 09-Mar-2022 09:41:13

Advertisements