pyspark.sql.functions.count_if

pyspark.sql.functions.count_if(col: ColumnOrName) → pyspark.sql.column.Column[source]

Returns the number of TRUE values for the col.

New in version 3.5.0.

Parameters
colColumn or str

target column to work on.

Returns
Column

the number of TRUE values for the col.

Examples

>>> df = spark.createDataFrame([("a", 1),
...                             ("a", 2),
...                             ("a", 3),
...                             ("b", 8),
...                             ("b", 2)], ["c1", "c2"])
>>> df.select(count_if(col('c2') % 2 == 0)).show()
+------------------------+
|count_if(((c2 % 2) = 0))|
+------------------------+
|                       3|
+------------------------+