1

I have a Spark dataframe with decimal column. I want to convert this column to a binary string. Are there any function for this can anybody help?

Thank you!

1
  • I just added an answer to another thread for doing this (converting a value to a String of binary digits) which works for Boolean, Byte, Short, Char, Int, and Long. stackoverflow.com/a/54950845/501113 Commented Mar 1, 2019 at 19:32

2 Answers 2

7

There is a bin inbuilt function which states

An expression that returns the string representation of the binary value of the given long column. For example, bin("12") returns "1100".

So if you have a dataframe as

+-----+
|Value|
+-----+
|4    |
+-----+

root
 |-- Value: decimal(10,0) (nullable = true)

You can use bin function as

import org.apache.spark.sql.functions._
data.withColumn("Value_Binary", bin(col("Value")))

which should give you

+-----+------------+
|Value|Value_Binary|
+-----+------------+
|4    |100         |
+-----+------------+

root
 |-- Value: decimal(10,0) (nullable = true)
 |-- Binary_value: string (nullable = true)
Sign up to request clarification or add additional context in comments.

Comments

0

I solved this issue with creating a user defined function.

val toBinStr: Int => String = _.toBinaryString

import org.apache.spark.sql.functions.udf
val toBinStrUDF = udf(toBinStr)

// Apply the UDF to change the source dataset
data.withColumn("Value_Binary", toBinStrUDF($"Value")).show

1 Comment

Yours only works with Int. I just added an answer to another thread for doing this (converting a value to a String of binary digits) which works for Boolean, Byte, Short, Char, Int, and Long. stackoverflow.com/a/54950845/501113

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.