pyspark.sql.functions.try_element_at(col: ColumnOrName, extraction: ColumnOrName) → pyspark.sql.column.Column[source]

(array, index) - Returns element of array at given (1-based) index. If Index is 0, Spark will throw an error. If index < 0, accesses elements from the last to the first. The function always returns NULL if the index exceeds the length of the array.

(map, key) - Returns value for given key. The function always returns NULL if the key is not contained in the map.

New in version 3.5.0.

colColumn or str

name of column containing array or map

extraction :

index to check for in array or key to check for in map


>>> df = spark.createDataFrame([(["a", "b", "c"],)], ['data'])
>>>, lit(1)).alias('r')).collect()
>>>, lit(-1)).alias('r')).collect()
>>> df = spark.createDataFrame([({"a": 1.0, "b": 2.0},)], ['data'])
>>>, lit("a")).alias('r')).collect()