Built with Tutify.io

    AIStudyLabs

    Polars Series len() Function with Examples

    Polars Series len() Function with Examples

    In Polars, the len() function returns the number of elements contained in a Series. A Polars Series is a single column of data, a named list of values, much like a column in a Pandas DataFrame.

    In this article, I will explain the syntax of the Polars Series len() function, its parameters, and how it returns the number of elements in the Series as an integer.

    Key Points –

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

    Polars Series len() Introduction

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

    
    # Syntax of len()
    Series.len() → int
    

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

    Parameters of the Polars Series len()

    No arguments are needed, just empty parentheses ().

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

    Return Value

    This function returns an integer (int) representing the number of elements in the Series.

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

    Usage of Polars Series len() Function

    The len() function retrieves the total number of elements (or rows) present in a Polars Series. It returns this count as an integer value.

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

    
    import polars as pl
    
    ser = pl.Series("numbers", [2, 4, 6, 8])
    print("Original Series:\n", ser)
    

    Yields below output.

    The basic usage of the Polars Series len() function involves calling it in the simplest way to retrieve the count of elements in a Series.

    
    # Get the length
    ser2 = ser.len()
    print("Length of Series:", ser2)
    

    Here,

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

    Series with Null Values

    The Series.len() function counts every element in the Series, including any null values. It returns the total number of rows, not just those with valid (non-null) data. In Polars, a null represents missing or undefined information. In Python, this is written as None, and it appears as null within Polars.

    
    import polars as pl
    
    # Series with null values
    ser = pl.Series("values", [2, None, 4, None, 6])
    
    # Series with null values
    ser2  =  ser.len()
    print("Length of Series:", ser2)
    
    # Output:
    # Length of Series: 5
    

    Here,

    <a id="h-series-with-strings"></a>

    Series with Strings

    A Series containing strings is essentially a column-like data structure that holds text values. Below is an example of a Polars Series with string entries and how to determine its length.

    
    import polars as pl
    
    # Create a Series of strings
    ser = pl.Series("fruits", ["apple", "banana", "mango", "grape"])
    
    # Get the length
    print("Length of series:", ser.len())
    
    # Output:
    # Length of series: 4
    

    Here,

    <a id="h-empty-series-in-polars"></a>

    Empty Series in Polars

    An empty Series is one that contains no elements at all. Below is an example of an empty Polars Series and how the len() function responds to it.

    
    import polars as pl
    
    # Create an empty Series
    ser = pl.Series("empty", [])
    print("Length of series:", ser.len())
    
    # output:
    # Length of series: 0
    

    Here,

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

    Conclusion

    In conclusion, the len() function in Polars is a simple yet essential tool for quickly determining the number of elements in a Series, including any null values. Whether your Series contains numbers, strings, or is empty, len() provides an accurate count to help you better understand and work with your data.

    Happy Learning!!

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

    Related Articles

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

    Reference