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 –
- The
null_count()method counts the number of null (missing) values in a Polars Series. - It returns a single integer indicating how many entries in the Series are null.
- This method does not modify the original Series; it only computes and reports the count.
null_count()can be called on Series of any data type (numeric, string, boolean, etc.).- If the Series has no missing values,
null_count()returns zero. - Can be combined with other methods like
drop_nulls()orfill_null()for further handling of missing data.
<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,
- The Series contains 5 elements.
- Two of them are
null, sonull_count()returns2.
<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,
- All entries (
10, 20, 30, 40, 50) are valid integers. - There are no
NoneorNaNvalues. null_count()returns0to confirm that no data is missing.
<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,
- All 4 entries are
None(Polars shows them asnull). null_count()correctly counts every element.- You get
4because all rows are missing data.
<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
- Polars Series len() Function with Examples
- Polars Series count() Function with Examples
- Polars Series mode() – Explained by Examples
- Polars Series product() Method with Examples
- Polars Series var() Usage & Examples
- Polars Series median() Function with Examples
- Polars Series std() – Explained by Examples
- Polars Series slice() – Usage & Examples
- Polars series quantile() – Usage & Examples
- Polars Series mean() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars Series limit() – Explained by Examples
- Polars Series fill_null() Function with Examples
<a id="h-reference"></a>
