pyspark.sql.functions.
array_position
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.
Column
target column to work on.
value to look for.
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)]