pyspark.sql.functions.get

pyspark.sql.functions.get(col: ColumnOrName, index: Union[ColumnOrName, int]) → pyspark.sql.column.Column[source]

Collection function: Returns element of array at given (0-based) index. If the index points outside of the array boundaries, then this function returns NULL.

New in version 3.4.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

name of column containing array

indexColumn or str or int

index to check for in array

Returns
Column

value at given position.

See also

element_at()

Notes

The position is not 1 based, but 0 based index.

Examples

>>> df = spark.createDataFrame([(["a", "b", "c"], 1)], ['data', 'index'])
>>> df.select(get(df.data, 1)).show()
+------------+
|get(data, 1)|
+------------+
|           b|
+------------+
>>> df.select(get(df.data, -1)).show()
+-------------+
|get(data, -1)|
+-------------+
|         null|
+-------------+
>>> df.select(get(df.data, 3)).show()
+------------+
|get(data, 3)|
+------------+
|        null|
+------------+
>>> df.select(get(df.data, "index")).show()
+----------------+
|get(data, index)|
+----------------+
|               b|
+----------------+
>>> df.select(get(df.data, col("index") - 1)).show()
+----------------------+
|get(data, (index - 1))|
+----------------------+
|                     a|
+----------------------+