pyspark.sql.functions.count

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

Aggregate function: returns the number of items in a group.

New in version 1.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

target column to compute on.

Returns
Column

column for computed results.

Examples

Count by all columns (start), and by a column that does not count None.

>>> df = spark.createDataFrame([(None,), ("a",), ("b",), ("c",)], schema=["alphabets"])
>>> df.select(count(expr("*")), count(df.alphabets)).show()
+--------+----------------+
|count(1)|count(alphabets)|
+--------+----------------+
|       4|               3|
+--------+----------------+