Loading...

verticapy.vDataColumn.mode

vDataColumn.mode(dropna: bool = False, n: int = 1) Annotated[bool | float | str | timedelta | datetime, 'Python Scalar']

This function returns the nth most frequently occurring element in the vDataColumn. It’s a practical method for identifying the element with a specific rank in terms of its occurrence frequency within the column. For example, you can use this function to find the third, fifth, or any other desired most frequent element.

Warning

This function first groups the data by a specific column, then computes the count of each group, and finally applies filtering. It’s important to note that this operation can be computationally expensive, especially for datasets with a large cardinality.

Parameters

dropna: bool, optional

If set to True, NULL values are not considered during the computation.

n: int, optional

Integer corresponding to the offset. For example, if n = 1, this method returns the mode of the vDataColumn.

Returns

PythonScalar

vDataColumn nth most occurent element.

Examples

For this example, let’s generate a dataset and calculate the mode of a column:

import verticapy as vp

data = vp.vDataFrame(
    {
        "x": [1, 2, 4, 9, 10, 15, 20, 22],
        "y": [1, 2, 1, 2, 1, 1, 2, 1],
        "z": [10, 12, 2, 1, 9, 8, 1, 3],
    }
)


data["y"].mode()
Out[3]: 1

Let’s now return the second most frequent element:

data["y"].mode(n = 2)
Out[4]: 2

Note

All the calculations are pushed to the database.

Hint

For more precise control, please refer to the aggregate method.

See also

vDataColumn.mean() : Mean for a specific column.
vDataFrame.median() : Median for particular columns.