pyspark.sql.functions.cardinality#

pyspark.sql.functions.cardinality(col)[source]#

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

Added in version 3.5.0.

Parameters:
colColumn or str

target column to compute on. A column that evaluates to an array or map.

Returns:
Column

length of the array/map. Returns a column that evaluates to an integer.

Examples

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