Pandas Tutorial Pandas References

Pandas DataFrame - mode() function



The Pandas DataFrame mode() function returns the mode(s) of each element over the specified axis. The syntax for using this function is mentioned below:

Syntax

DataFrame.mode(axis=0, numeric_only=False, dropna=True)

Parameters

axis Optional. Specify {0 or 'index', 1 or 'columns'}. If 0 or 'index', mode of the values are generated for each column. If 1 or 'columns', mode of the values are generated for each row. Default: 0
numeric_only Optional. Specify True to include only float, int or boolean data. Default: False
dropna Optional. Specify True to exclude NA/null values when computing the result. Default is True.

Return Value

Returns modes of each column or row.

Example: using mode() column-wise on whole DataFrame

In the example below, a DataFrame df is created. The mode() function is used to get the mode of values for each column.

import pandas as pd
import numpy as np

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

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

#mode of values of all entries column-wise
print("\ndf.mode() returns:")
print(df.mode())

The output of the above code will be:

The DataFrame is:
       Bonus  Last Bonus  Last Salary  Salary
John       5           5           58      60
Marry      2           2           59      60
Sam        2           2           63      64
Jo         4           4           58      59

df.mode() returns:
   Bonus  Last Bonus  Last Salary  Salary
0      2           2           58      60

Example: using mode() row-wise on whole DataFrame

To perform the operation row-wise, the axis parameter can be set to 1.

import pandas as pd
import numpy as np

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

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

#mode of values of all entries row-wise
print("\ndf.mode(axis=1) returns:")
print(df.mode(axis=1))

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.mode(axis=1) returns:
John     32.5
Marry    32.5
Sam      33.5
Jo       31.5
dtype: float64

Example: using mode() on selected column

Instead of whole DataFrame, the mode() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

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

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

#mode of values of single column
print("\ndf['Salary'].mode() returns:")
print(df["Salary"].mode())

#mode of values of multiple columns
print("\ndf[['Salary', 'Bonus']].mode() returns:")
print(df[["Salary", "Bonus"]].mode())

The output of the above code will be:

The DataFrame is:
       Bonus  Last Bonus  Last Salary  Salary
John       5           5           58      60
Marry      2           2           59      60
Sam        2           2           63      64
Jo         4           4           58      59

df['Salary'].mode() returns:
0    60
dtype: int64

df[['Salary', 'Bonus']].mode() returns:
   Salary  Bonus
0      60      2

❮ Pandas DataFrame - Functions