Built with Tutify.io

    AIStudyLabs

    Polars Series mode() – Explained by Examples

    Polars Series mode() – Explained by Examples

    In Polars, the Series.mode() method returns the most frequently occurring value(s) in a Series, statistically known as the mode. It is used to identify the value(s) that appear most frequently within the Series.

    In this article, I will explain the syntax of the Polars Series mode() function, its parameters, and how to use it to retrieve the most frequent value(s) in a Series.

    Key Points –

    <a id="h-polars-series-mode-introduction"></a>

    Polars Series mode() Introduction

    Let’s know the syntax of the series mode() function.

    
    # Syntax of mode()
    Series.mode() → Series
    

    <a id="h-parameters-of-the-polars-series-mode"></a>

    Parameters of the Polars Series mode()

    A Polars Series (e.g., numbers, strings, etc.)

    <a id="h-return-value"></a>

    Return Value

    This method returns a new Polars Series containing the most frequent value(s) in the original Series.

    <a id="h-usage-of-polars-series-mode-function"></a>

    Usage of Polars Series mode() Function

    The mode() method in Polars identifies the value(s) that appear most frequently in a Series. It is used to determine the most common data point(s) within a column.

    Now, let’s create a Polars series using a list of values and use the mode() function.

    
    import polars as pl
    
    ser = pl.Series("values", [10, 20, 20, 30])
    print("Original Series:\n", ser)
    

    Yields below output.

    To demonstrate a numeric Polars Series with a single mode, you can apply the mode() method to a Series where one value appears more frequently than the others.

    
    # Apply mode()
    ser2 = ser.mode()
    print("Mode of the Series:\n", ser2)
    

    Here,

    <a id="h-numeric-series-with-multiple-modes"></a>

    Numeric Series with Multiple Modes

    You can demonstrate the use of Polars Series.mode() on a numeric Series with multiple modes, where two or more values occur with the same highest frequency.

    
    import polars as pl
    
    # Create a numeric Series with multiple modes
    ser = pl.Series("values", [10, 20, 20, 30, 10])
    
    # Find the mode
    ser2 = ser.mode()
    print("Mode of the Series:\n", ser2)
    
    # Output:
    # Mode of the Series:
    # shape: (2,)
    # Series: 'values' [i64]
    # [
    #	20
    #	10
    # ]
    

    Here,

    <a id="h-series-with-nulls-nulls-are-ignored"></a>

    Series with Nulls (Nulls are Ignored)

    When using the mode() method on a Polars Series that contains null values, Polars automatically ignores the nulls during the calculation. You can demonstrate this behavior by applying mode() to a Series with missing values.

    
    import polars as pl
    
    # Series with numeric values and nulls
    ser = pl.Series("values", [10, 20, None, 20, None, 30])
    
    # Find the mode
    ser2 = ser.mode()
    print("Mode of the Series (nulls ignored):\n", ser2)
    
    # Output:
    # Mode of the Series (nulls ignored):
    # shape: (2,)
    # Series: 'values' [i64]
    # [
    #	20
    # ]
    

    Here,

    <a id="h-series-with-all-unique-values"></a>

    Series with All Unique Values

    You can demonstrate the mode() method on a Polars Series where every value is unique, that is, no duplicates exist. This shows how mode() behaves when there are no repeating values in the Series.

    
    import polars as pl
    
    # Create a Series where all values are unique
    ser = pl.Series("values", [10, 20, 30, 40])
    
    # Apply mode()
    ser2 = ser.mode()
    print("Mode of the Series (all values unique):\n", ser2)
    
    # Output:
    # Mode of the Series (all values unique):
    # shape: (4,)
    # Series: 'values' [i64]
    # [
    #	30
    #	10
    #	40
    #	20
    # ]
    

    Here,

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, the Series.mode() method in Polars is a useful tool for identifying the most frequently occurring value(s) in a Series. Whether the data contains a single mode, multiple modes, null values, or entirely unique entries, Polars handles each case gracefully.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference