Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2024 05:28 AM
Hi @Harsha777 ,
Your solution looks good!
However, you may try also to_number function, but unfortunately still will need to first to replace "," with ".".
from pyspark.sql.functions import to_number, regexp_replace, lit
data = [("10,6523",), ("10,23",)]
df = spark.createDataFrame(data, ["col_name"])
df = df.withColumn("decimal_col", to_number(regexp_replace("col_name", ",", "."), lit("9999999.9999")))
df.show()Without replacement it will just remove the comma:
from pyspark.sql.functions import to_number, lit
data = [("10,6523",), ("10,23",)]
df = spark.createDataFrame(data, ["col_name"])
df = df.withColumn("decimal_col", to_number("col_name", lit("99,9999")))
df.show()