pyspark.sql.functions.array_position

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

Collection function: Locates the position of the first occurrence of the given value in the given array. Returns null if either of the arguments are null.

New in version 2.4.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

target column to work on.

valueAny

value to look for.

Returns
Column

position of the value in the given array if found and 0 otherwise.

Notes

The position is not zero based, but 1 based index. Returns 0 if the given value could not be found in the array.

Examples

>>> df = spark.createDataFrame([(["c", "b", "a"],), ([],)], ['data'])
>>> df.select(array_position(df.data, "a")).collect()
[Row(array_position(data, a)=3), Row(array_position(data, a)=0)]