You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by frakass <ca...@free.fr> on 2022/02/11 12:29:02 UTC
how to classify column
Hello
I have a column whose value (Int type as score) is from 0 to 5.
I want to query that, when the score > 3, classified as "good". else
classified as "bad".
How do I implement that? A UDF like something as this?
scala> implicit class Foo(i:Int) {
| def classAs(f:Int=>String) = f(i)
| }
class Foo
scala> 4.classAs { x => if (x > 3) "good" else "bad" }
val res13: String = good
scala> 2.classAs { x => if (x > 3) "good" else "bad" }
val res14: String = bad
Thank you.
---------------------------------------------------------------------
To unsubscribe e-mail: user-unsubscribe@spark.apache.org
Re: how to classify column
Posted by frakass <ca...@free.fr>.
that's good. thanks
On 2022/2/12 12:11, Raghavendra Ganesh wrote:
> .withColumn("newColumn",expr(s"case when score>3 then 'good' else 'bad'
> end"))
>
---------------------------------------------------------------------
To unsubscribe e-mail: user-unsubscribe@spark.apache.org
Re: how to classify column
Posted by Raghavendra Ganesh <ra...@gmail.com>.
You could use expr() function to achieve the same.
.withColumn("newColumn",expr(s"case when score>3 then 'good' else 'bad'
end"))
--
Raghavendra
On Fri, Feb 11, 2022 at 5:59 PM frakass <ca...@free.fr> wrote:
> Hello
>
> I have a column whose value (Int type as score) is from 0 to 5.
> I want to query that, when the score > 3, classified as "good". else
> classified as "bad".
> How do I implement that? A UDF like something as this?
>
> scala> implicit class Foo(i:Int) {
> | def classAs(f:Int=>String) = f(i)
> | }
> class Foo
>
> scala> 4.classAs { x => if (x > 3) "good" else "bad" }
> val res13: String = good
>
> scala> 2.classAs { x => if (x > 3) "good" else "bad" }
> val res14: String = bad
>
>
> Thank you.
>
> ---------------------------------------------------------------------
> To unsubscribe e-mail: user-unsubscribe@spark.apache.org
>
>