Pandas Tutorial Pandas References

Pandas Series - mod() function



The Pandas mod() function returns modulo of series and other, element-wise. It is equivalent to series % other, but with support to substitute a fill_value for missing data as one of the parameters.

Syntax

Series.mod(other, level=None, fill_value=None)

Parameters

other Required. Specify scalar value or Series.
level Optional. Specify int or name to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None.
fill_value Optional. Specify value to fill existing missing (NaN) values, and any new element needed for successful Series alignment. If data in both corresponding Series locations is missing the result will be missing. Default is None.

Return Value

Returns the result of the arithmetic operation.

Example: Dividing the Series with a scalar value

In the example below, the mod() function is used to calculate modulo of the Series when divided by a given scalar value.

import pandas as pd
import numpy as np

x = pd.Series([10, 20, 30, 40, 50])

print("The Series contains:")
print(x)

#dividing the Series by 3
print("\nx.mod(3) returns:")
print(x.mod(3))

The output of the above code will be:

The Series contains:
0    10
1    20
2    30
3    40
4    50
dtype: int64

x.mod(3) returns:
0    1
1    2
2    0
3    1
4    2
dtype: int64

Example: Dividing two Series

The modulo of the Series can be calculated by dividing it with another series. Consider the following example:

import pandas as pd
import numpy as np

x = pd.Series([11, np.NaN, 13, 14], 
              index=['A', 'B', 'C', 'D'])
y = pd.Series([5, 6, 7, np.NaN], 
              index=['A', 'B', 'C', 'D'])

print("The x contains:")
print(x)

print("\nThe y contains:")
print(y)

#dividing x by y
print("\nx.mod(y, fill_value=0) returns:")
print(x.mod(y, fill_value=0))

The output of the above code will be:

The x contains:
A    11.0
B     NaN
C    13.0
D    14.0
dtype: float64

The y contains:
A    5.0
B    6.0
C    7.0
D    NaN
dtype: float64

x.mod(y, fill_value=0) returns:
A    1.0
B    0.0
C    6.0
D    NaN
dtype: float64

Example: Dividing columns in a DataDrame

The mod() function can be applied in a DataFrame to get the modulo of two series/column element-wise. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Dividend": [10, 20, 30, 40, 50],
  "Divisor": [5, 6, 7, 8, 9]
})

print("The DataFrame is:")
print(df)

#dividing 'Dividend' by 'Divisor' column
df['Remainder'] = df['Dividend'].mod(df['Divisor'])

print("\nThe DataFrame is:")
print(df)

The output of the above code will be:

The DataFrame is:
   Dividend  Divisor
0        10        5
1        20        6
2        30        7
3        40        8
4        50        9

The DataFrame is:
   Dividend  Divisor  Remainder
0        10        5          0
1        20        6          2
2        30        7          2
3        40        8          0
4        50        9          5

❮ Pandas Series - Functions