Facebook Page Twitter Page LinkedIn Page
× Pandas Tutorial Pandas Resources


The Pandas mod() function is used to get modulo of dataframe and argument, element-wise (binary operator mod). It is equivalent to dataframe % other, but with support to substitute a fill_value for missing data as one of the parameters. The syntax for using this function is given below:

Syntax

DataFrame.mod(other, axis='columns', level=None, fill_value=None)

Parameters

other Required. Specify any single or multiple element data structure, or list-like object.
axis Optional. Specify whether to compare by the index (0 or 'index') or columns (1 or 'columns'). For Series input, axis to match Series index on. Default is 'columns'.
level Optional. Specify int or label 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 DataFrame alignment. If data in both corresponding DataFrame locations is missing the result will be missing. Default is None.

Return Value

Returns the result of the arithmetic operation.

Example: Using mod() on whole DataFrame

In the example below, a DataFrame df is created. The mod() function is used to calculate modulo of the whole data frame when divided by a given scalar value.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

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

#Modulo the DataFrame is divided by 2
print("\ndf.mod(2) returns:")
print(df.mod(2))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.mod(2) returns:
       Bonus  Salary
John       1       0
Marry      1       0
Sam        0       1
Jo         0       1

Example: Using different scalar value for different column

Different column can be divided by different scalar value to calculate the modulo by providing other argument as a list. Consider the following example:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

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

#Modulo when Bonus column is divided by 2
#Modulo when Salary column is divided by 10
print("\ndf.mod([2,10]) returns:")
print(df.mod([2,10]))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.mod([2,10]) returns:
       Bonus  Salary
John       1       0
Marry      1       2
Sam        0       5
Jo         0       9

Example: Using mod() on selected columns

Instead of whole data frame, the mod() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Last Salary": [58, 60, 63, 57],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

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

#Modulo when Salary column is divided by 3
print("\ndf['Salary'].mod(3) returns:")
print(df["Salary"].mod(3))

#Modulo when Salary column is divided by 3
#Modulo when Bonus column is divided by 2
print("\ndf[['Salary', 'Bonus']].mod([3,2]) returns:")
print(df[["Salary", "Bonus"]].mod([3,2]))

The output of the above code will be:

The DataFrame is:
       Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

df['Salary'].mod(3) returns:
John     0
Marry    2
Sam      2
Jo       2
Name: Salary, dtype: int64

df[['Salary', 'Bonus']].mod([3,2]) returns:
       Salary  Bonus
John        0      1
Marry       2      1
Sam         2      0
Jo          2      0

Example: Dividing columns in a DataDrame

The mod() function can be applied in a data frame 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 DataFrame - Functions

5