diff --git a/python/pyspark/sql/functions/builtin.py b/python/pyspark/sql/functions/builtin.py index d82927b7af04..2a302d1e5112 100644 --- a/python/pyspark/sql/functions/builtin.py +++ b/python/pyspark/sql/functions/builtin.py @@ -14098,10 +14098,13 @@ def element_at(col: "ColumnOrName", extraction: Any) -> Column: Notes ----- The position is not zero based, but 1 based index. + If extraction is a string, :meth:`element_at` treats it as a literal string, + while :meth:`try_element_at` treats it as a column name. See Also -------- :meth:`get` + :meth:`try_element_at` Examples -------- @@ -14148,6 +14151,17 @@ def element_at(col: "ColumnOrName", extraction: Any) -> Column: +-------------------+ | NULL| +-------------------+ + + Example 5: Getting a value from a map using a literal string as the key + + >>> from pyspark.sql import functions as sf + >>> df = spark.createDataFrame([({"a": 1.0, "b": 2.0}, "a")], ['data', 'b']) + >>> df.select(sf.element_at(df.data, 'b')).show() + +-------------------+ + |element_at(data, b)| + +-------------------+ + | 2.0| + +-------------------+ """ return _invoke_function_over_columns("element_at", col, lit(extraction)) @@ -14172,6 +14186,17 @@ def try_element_at(col: "ColumnOrName", extraction: "ColumnOrName") -> Column: extraction : index to check for in array or key to check for in map + Notes + ----- + The position is not zero based, but 1 based index. + If extraction is a string, :meth:`try_element_at` treats it as a column name, + while :meth:`element_at` treats it as a literal string. + + See Also + -------- + :meth:`get` + :meth:`element_at` + Examples -------- Example 1: Getting the first element of an array @@ -14228,6 +14253,17 @@ def try_element_at(col: "ColumnOrName", extraction: "ColumnOrName") -> Column: +-----------------------+ | NULL| +-----------------------+ + + Example 6: Getting a value from a map using a column name as the key + + >>> from pyspark.sql import functions as sf + >>> df = spark.createDataFrame([({"a": 1.0, "b": 2.0}, "a")], ['data', 'b']) + >>> df.select(sf.try_element_at(df.data, 'b')).show() + +-----------------------+ + |try_element_at(data, b)| + +-----------------------+ + | 1.0| + +-----------------------+ """ return _invoke_function_over_columns("try_element_at", col, extraction)