pyspark.sql.functions.cardinality

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

Collection function: returns the length of the array or map stored in the column.

New in version 3.5.0.

Parameters
colColumn or str

target column to compute on.

Returns
Column

length of the array/map.

Examples

>>> import pyspark.sql.functions as sf
>>> spark.createDataFrame(
...     [([1, 2, 3],),([1],),([],)], ['data']
... ).select(sf.cardinality("data")).show()
+-----------------+
|cardinality(data)|
+-----------------+
|                3|
|                1|
|                0|
+-----------------+