pyspark.sql.functions.array_prepend

pyspark.sql.functions.array_prepend(col: ColumnOrName, value: Any) → pyspark.sql.column.Column[source]

Collection function: Returns an array containing element as well as all elements from array. The new element is positioned at the beginning of the array.

New in version 3.5.0.

Parameters
colColumn or str

name of column containing array

value

a literal value, or a Column expression.

Returns
Column

an array excluding given value.

Examples

>>> df = spark.createDataFrame([([2, 3, 4],), ([],)], ['data'])
>>> df.select(array_prepend(df.data, 1)).collect()
[Row(array_prepend(data, 1)=[1, 2, 3, 4]), Row(array_prepend(data, 1)=[1])]