count_if
Returns the number of TRUE values for the column.
Syntax
Python
from pyspark.sql import functions as sf
sf.count_if(col)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Target column to work on. |
Returns
pyspark.sql.Column: the number of TRUE values for the col.
Examples
Example 1: Counting the number of even numbers in a numeric column
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame([("a", 1), ("a", 2), ("a", 3), ("b", 8), ("b", 2)], ["c1", "c2"])
df.select(sf.count_if(sf.col('c2') % 2 == 0)).show()
Output
+------------------------+
|count_if(((c2 % 2) = 0))|
+------------------------+
| 3|
+------------------------+
Example 2: Counting the number of rows where a string column starts with a certain letter
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame(
[("apple",), ("banana",), ("cherry",), ("apple",), ("banana",)], ["fruit"])
df.select(sf.count_if(sf.col('fruit').startswith('a'))).show()
Output
+------------------------------+
|count_if(startswith(fruit, a))|
+------------------------------+
| 2|
+------------------------------+
Example 3: Counting the number of rows where a numeric column is greater than a certain value
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame([(1,), (2,), (3,), (4,), (5,)], ["num"])
df.select(sf.count_if(sf.col('num') > 3)).show()
Output
+-------------------+
|count_if((num > 3))|
+-------------------+
| 2|
+-------------------+
Example 4: Counting the number of rows where a boolean column is True
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame([(True,), (False,), (True,), (False,), (True,)], ["b"])
df.select(sf.count('b'), sf.count_if('b')).show()
Output
+--------+-----------+
|count(b)|count_if(b)|
+--------+-----------+
| 5| 3|
+--------+-----------+