pyspark.sql.functions.array_position(col, value)[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.


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


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