Built with Tutify.io

    AIStudyLabs

    Polars Series count() Function with Examples

    Polars Series count() Function with Examples

    In Polars, the series.count() function is used to count the number of non-null (non-missing) values present in that Series. It excludes null (missing) values and counts only the valid data points.

    In this article, I will explain the syntax of the Polars Series count() function, its parameters, and how it returns an integer (int) representing the number of non-null values in the Series.

    Key Points –

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

    Polars Series count() Introduction

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

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

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

    Parameters of the Polars Series count()

    No parameters. This is a method called directly on a Series object.

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

    Return Value

    This method returns an int representing the number of non-null (valid) elements in the Series.

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

    Usage of Polars Series count() Function

    The count() function in a Polars Series returns the number of non-null (non-missing) values present in the Series.

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

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

    Yields below output.

    To perform a simple count of non-null values in a Polars Series, use the count() method.

    
    # Count non-null values
    ser2 = ser.count()
    print("Non-null value count:", ser2)
    

    Here,

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

    Series with Null Values

    You can create a Polars Series containing null values and use the count() function to determine the number of non-null entries.

    
    import polars as pl
    
    # Series with null (None) values
    ser = pl.Series("values", [5, None, 15, None, 25])
    
    # Count non-null values
    ser2 = ser.count()
    print("Non-null value count:", ser2)
    
    # Output:
    # Non-null value count: 3
    

    Here,

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

    Series of Strings with Nulls

    You can create a Polars Series of strings that contains null values and use the count() function to see how it handles non-null entries.

    
    import polars as pl
    
    # Create a Series of strings with some null values
    ser = pl.Series("Tech", ["Spark", None, "Pandas", "Hadoop", None])
    
    # Count non-null values
    ser2 = ser.count()
    print("Non-null value count:", ser2)
    
    # Output:
    # Non-null value count: 3
    

    Here,

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

    Series with Booleans and Null

    You can create a Polars Series containing Boolean values along with nulls and use the count() function to observe how it handles non-null Boolean entries.

    
    import polars as pl
    
    # Boolean Series with nulls
    ser = pl.Series("flags", [True, None, False, True, None])
    print("Non-null count:", ser.count())
    
    # Output:
    # Non-null count: 3
    

    Here,

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

    Conclusion

    In conclusion, the count() function in Polars Series is a simple yet powerful tool to determine the number of non-null (valid) values in your data. Whether you're working with strings, booleans, or any other data type, count() helps you quickly assess the presence of meaningful data by ignoring nulls.

    Happy Learning!!

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

    Related Articles

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

    Reference