You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Marco Mistroni <mm...@gmail.com> on 2016/08/25 21:09:33 UTC

Converting DataFrame's int column to Double

hi all
 i might be stuck in old code, but this is what i am doing to convert  a DF
int column to Double

val intToDoubleFunc:(Int => Double) = lbl => lbl.toDouble
val labelToDblFunc = udf(intToDoubleFunc)


val convertedDF = df.withColumn("SurvivedDbl",
labelToDblFunc(col("Survived")))

is there a  better way to do that?

kr

Re: Converting DataFrame's int column to Double

Posted by Marco Mistroni <mm...@gmail.com>.
many tx Jestin!

On Thu, Aug 25, 2016 at 10:13 PM, Jestin Ma <je...@gmail.com>
wrote:

> How about this:
>
> df.withColumn("doubles", col("ints").cast("double")).drop("ints")
>
> On Thu, Aug 25, 2016 at 2:09 PM, Marco Mistroni <mm...@gmail.com>
> wrote:
>
>> hi all
>>  i might be stuck in old code, but this is what i am doing to convert  a
>> DF int column to Double
>>
>> val intToDoubleFunc:(Int => Double) = lbl => lbl.toDouble
>> val labelToDblFunc = udf(intToDoubleFunc)
>>
>>
>> val convertedDF = df.withColumn("SurvivedDbl",
>> labelToDblFunc(col("Survived")))
>>
>> is there a  better way to do that?
>>
>> kr
>>
>>
>
>

Re: Converting DataFrame's int column to Double

Posted by Jestin Ma <je...@gmail.com>.
How about this:

df.withColumn("doubles", col("ints").cast("double")).drop("ints")

On Thu, Aug 25, 2016 at 2:09 PM, Marco Mistroni <mm...@gmail.com> wrote:

> hi all
>  i might be stuck in old code, but this is what i am doing to convert  a
> DF int column to Double
>
> val intToDoubleFunc:(Int => Double) = lbl => lbl.toDouble
> val labelToDblFunc = udf(intToDoubleFunc)
>
>
> val convertedDF = df.withColumn("SurvivedDbl",
> labelToDblFunc(col("Survived")))
>
> is there a  better way to do that?
>
> kr
>
>