Built with Tutify.io

    AIStudyLabs

    Polars Series null_count() – Usage & Examples

    Polars Series null_count() – Usage & Examples

    In Polars, the null_count() method is used to count the number of null (missing) values in a Series. It helps identify how many entries are missing. In this article, I will explain the syntax of the Polars Series null_count() function, describe its parameters, and show how it returns an integer representing the number of null (missing) values in the Series.

    Key Points –

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

    Polars Series null_count() Introduction

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

    
    # Syntax of series null_count
    Series.null_count() → int
    

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

    Parameters of the Polars Series null_count()

    No arguments needed.

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

    Return Value

    This function returns an integer representing how many null values are in the Series.

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

    Usage of Polars Series null_count() Function

    null_count() calculates how many null (missing) values are present in a Polars Series and returns that count as an integer.

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

    
    import polars as pl
    
    # Create a Series with integer values and some nulls
    ser = pl.Series("values", [5, None, 15, None, 25])
    print("Original Series:\n", ser)
    

    Yields below output.

    Here’s a simple example demonstrating how to use null_count() with a Polars Series containing integers.

    
    # Count the number of nulls
    ser2 = ser.null_count()
    print("Number of null values:", ser2)
    

    Here,

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

    Series with All Non-null Values

    You can create a Polars Series with only non-null values and use the null_count() function to check how many null entries it contains.

    
    import polars as pl
    
    # Series with no nulls
    ser = pl.Series("numbers", [10, 20, 30, 40, 50])
    
    # Count nulls
    ser2 = ser.null_count()
    print("Number of null values:", ser2)
    
    # Output:
    # Number of null values: 0
    

    Here,

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

    Series with All Null Values

    Polars Series where all values are null and how null_count() behaves, here’s a demonstration of using null_count() on a Series consisting entirely of null values.

    
    import polars as pl
    
    # Series where every element is null
    ser = pl.Series("empty_values", [None, None, None, None])
    
    # Count the nulls
    ser2 = ser.null_count()
    print("Number of null values:", ser2)
    
    # Output:
    # Number of null values: 4
    

    Here,

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

    Conclusion

    In conclusion, the null_count() method in Polars is a straightforward way to identify the number of missing values in a Series. Whether your data contains no nulls or is entirely null, this function provides an accurate count to help you assess data completeness.

    Happy Learning!!

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

    Related Articles

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

    Reference