Himanshu Malhotra
Himanshu Malhotra

Reputation: 51

How to format number column in pyspark?

I want to format the number of a column to comma separated ( currency format ).

for example - i have column

input column

the output should be

output column

I have tried using '{:,.2f}'.format(col("value")) but i am unable to apply this function by creating udf.

NOTE: There are also null values present in the column.

Upvotes: 3

Views: 14351

Answers (1)

Lamanus
Lamanus

Reputation: 13581

There is the format_number function that makes the currency format with the rounded number up to your second argument.

import pyspark.sql.functions as F

df.withColumn('format_num', F.format_number('num', 0)) \
  .show(10, False)

+---+--------+----------+
|id |num     |format_num|
+---+--------+----------+
|1  |123525  |123,525   |
|2  |13245123|13,245,123|
|3  |null    |null      |
+---+--------+----------+

Upvotes: 8

Related Questions