MODE
This function returns the mode, the most frequently occurring numeric value, from a 2D dataset.
For frequency counts f(v) over unique values v, the mode is any value maximizing frequency:
v_{\text{mode}} \in \arg\max_{v} f(v)
In this wrapper, non-numeric cells are ignored and an error is returned when no value repeats (that is, when no mode exists under the selected rule).
Excel Usage
=MODE(data)
data(list[list], required): 2D array of numeric values to find the mode of. Non-numeric values are ignored.
Returns (float): The most common value in the data, or str error message if invalid.
Example 1: Simple mode with repeated value
Inputs:
| data | |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
Excel formula:
=MODE({1,2;2,3;2,4})
Expected output:
2
Example 2: Multiple modes returns smallest
Inputs:
| data | |
|---|---|
| 1 | 2 |
| 2 | 1 |
| 3 | 3 |
Excel formula:
=MODE({1,2;2,1;3,3})
Expected output:
1
Example 3: Non-numeric values ignored
Inputs:
| data | |
|---|---|
| 1 | a |
| 2 | 2 |
| 2 | b |
Excel formula:
=MODE({1,"a";2,2;2,"b"})
Expected output:
2
Example 4: Mode of a larger array
Inputs:
| data | ||
|---|---|---|
| 10 | 20 | 30 |
| 20 | 40 | 50 |
| 60 | 20 | 80 |
Excel formula:
=MODE({10,20,30;20,40,50;60,20,80})
Expected output:
20
Python Code
from scipy.stats import mode as scipy_mode
def mode(data):
"""
Return the modal (most common) numeric value in the input data, returning the smallest if there are multiple modes.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mode.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): 2D array of numeric values to find the mode of. Non-numeric values are ignored.
Returns:
float: The most common value in the data, or str error message if invalid.
"""
# Helper function to normalize scalar input to 2D list
def to2d(x):
return [[x]] if not isinstance(x, list) else x
try:
# Normalize input for 2D list args
data = to2d(data)
# If input is not a list of lists, return error
if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
return "Error: Invalid input: data must be a 2D list."
# Flatten and filter numeric values
flat = []
for row in data:
for val in row:
try:
v = float(val)
flat.append(v)
except (ValueError, TypeError):
continue
if len(flat) < 2:
return "Error: Invalid input: data must contain at least two numeric values."
# Use scipy.stats.mode
res = scipy_mode(flat, keepdims=False, axis=None, nan_policy='omit')
if res.count < 2:
return "Error: No mode found. At least one value must appear more than once."
# Return as Python float scalar, not numpy type
mode_value = float(res.mode.item()) if hasattr(res.mode, 'item') else float(res.mode)
return mode_value
except Exception as e:
return f"Error: {str(e)}"Online Calculator
2D array of numeric values to find the mode of. Non-numeric values are ignored.