You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by wilson <wi...@4shield.net> on 2022/05/23 06:04:08 UTC

how to add a column for percent

hello

how to add a column for percent for the current row of counted data?

scala>
df2.groupBy("_c1").count.withColumn("percent",f"${col(count)/df2.count}%.2f").show

<console>:30: error: type mismatch;


This doesn't work.

so please help. thanks.

Re: how to add a column for percent

Posted by Raghavendra Ganesh <ra...@gmail.com>.
withColumn takes a column as the second argument, not string.
If you want formatting before show() you can use the round() function.
--
Raghavendra


On Mon, May 23, 2022 at 11:35 AM wilson <wi...@4shield.net> wrote:

> hello
>
> how to add a column for percent for the current row of counted data?
>
> scala>
> df2.groupBy("_c1").count.withColumn("percent",f"${col(count)/df2.count}%.2f").show
>
> <console>:30: error: type mismatch;
>
>
> This doesn't work.
>
> so please help. thanks.
>
>
>
>