pyspark.sql.functions.
get
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.
Column
name of column containing array
index to check for in array
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| +----------------------+