Yi Du
Yi Du

Reputation: 525

How to convert number into percentage

For a pyspark data frame, Do you know how to convert numbers with decimals into percentage format? I can even determine the number of decimal points I want to keep.

Upvotes: 3

Views: 7386

Answers (1)

notNull
notNull

Reputation: 31490

You can multiply with 100

df.withColumn("rate",(col("rate") * 100).cast("int")).show()
+---+---+----+
| id|row|rate|
+---+---+----+
|  A|  1|   1|
+---+---+----+

df.withColumn("rate",concat((col("rate") * 100).cast("int"),lit('%'))).show()
+---+---+----+
| id|row|rate|
+---+---+----+
|  A|  1|  1%|
+---+---+----+

Upvotes: 5

Related Questions