pyspark.sql.functions.str_to_map

pyspark.sql.functions.str_to_map(text: ColumnOrName, pairDelim: Optional[ColumnOrName] = None, keyValueDelim: Optional[ColumnOrName] = None) → pyspark.sql.column.Column[source]

Creates a map after splitting the text into key/value pairs using delimiters. Both pairDelim and keyValueDelim are treated as regular expressions.

New in version 3.5.0.

Parameters
textColumn or str

Input column or strings.

pairDelimColumn or str, optional

delimiter to use to split pair.

keyValueDelimColumn or str, optional

delimiter to use to split key/value.

Examples

>>> df = spark.createDataFrame([("a:1,b:2,c:3",)], ["e"])
>>> df.select(str_to_map(df.e, lit(","), lit(":")).alias('r')).collect()
[Row(r={'a': '1', 'b': '2', 'c': '3'})]
>>> df = spark.createDataFrame([("a:1,b:2,c:3",)], ["e"])
>>> df.select(str_to_map(df.e, lit(",")).alias('r')).collect()
[Row(r={'a': '1', 'b': '2', 'c': '3'})]
>>> df = spark.createDataFrame([("a:1,b:2,c:3",)], ["e"])
>>> df.select(str_to_map(df.e).alias('r')).collect()
[Row(r={'a': '1', 'b': '2', 'c': '3'})]