pyspark.sql.functions.try_to_number

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

Convert string ‘col’ to a number based on the string format format. Returns NULL if the string ‘col’ does not match the expected format. The format follows the same semantics as the to_number function.

New in version 3.5.0.

Parameters
colColumn or str

Input column or strings.

formatColumn or str, optional

format to use to convert number values.

Examples

>>> df = spark.createDataFrame([("$78.12",)], ["e"])
>>> df.select(try_to_number(df.e, lit("$99.99")).alias('r')).collect()
[Row(r=Decimal('78.12'))]