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 –
- The
count()function returns the number of non-null (non–missing) values in a Series. - The result is an integer (
int) representing the total valid entries. count()works with all data types, including strings, integers, floats, booleans, etc.- It is useful for data validation, quality checks, and handling missing data.
- The function is applied directly to a Series object in Polars.
- It ignores nulls, making it useful for measuring data completeness.
Polars Series count() Introduction
Let’s know the syntax of the series count() function.
# Syntax of count()
Series.count() → int
Parameters of the Polars Series count()
No parameters. This is a method called directly on a Series object.
Return Value
This method returns an int representing the number of non-null (valid) elements in the Series.
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,
- Since there are no nulls,
count()returns 4, the total number of elements.
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,
Noneis automatically treated asnullin Polars.count()ignores nulls and only returns the count of actual (non-null) values.
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,
- The Series contains 5 elements, 2 of which are
null. count()ignoresnulland returns the count of valid (non-null) strings,3.
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,
- The Series contains 5 boolean entries, including 2
nulls. count()skips the nulls and returns3, the number of non-null boolean values (True,False,True).
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!!
Related Articles
- 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 interpolate() Function
- Polars Series limit() – Explained by Examples
- Polars Series fill_null() Function with Examples