Python statistics.mode() Function



The Python statistics.mode() function returns the single most common data point from the discrete or nominal data. This function calculates the central tendency from the numerical dataset.

In this function if there are multiple modes with the same frequency, then this function returns the first one in the dataset.

Mode assumes a discrete dataset and returns a single value.

Syntax

Following is the basic syntax for the statistics.mode() function −

statistics.mode(data)

Parameters

Here, the data values can be used as any sequence, list or iterator.

Return Value

This function returns the most common first element from the dataset.

Example 1

In the below example we are calculating the mode (central tendency) of the given data using statistics.mode() function.

import statistics
x = statistics.mode([2, 4, 4, 4, 6, 7, 7, 8])
print(x)

Output

This produces the following result −

4

Example 2

We are calculating mode for the non-numeric dataset using statistics.mode() function.

import statistics
x = statistics.mode(['blue', 'red', 'blue', 'yellow', 'blue', 'red'])
print(x)

Output

The result produced is as follows −

blue

Example 3

Here, we are calculating negative numbers in the dataset using statistics.mode() function.

import statistics
x = statistics.mode([-1, 2, -1, 3, -1, -2, -1])
print(x)

Output

The output obtained is as follows −

-1
python_modules.htm
Advertisements