So I have a dataframe with one column like this:
+----------+
|some_colum|
+----------+
| 10|
| 00|
| 00|
| 10|
| 10|
| 00|
| 10|
| 00|
| 00|
| 10|
+----------+
where the column some_colum are binary strings.
I want to convert this column to decimal.
I've tried doing
data = data.withColumn("some_colum", int(col("some_colum"), 2))
But this doesn't seem to work. as I get the error:
int() can't convert non-string with explicit base
I think cast() might be able to do the job but I'm unable to figure it out. Any ideas?
I think the int
cannot be applied directly to a column. You can use in a udf:
from org.apache.spark.sql import functions
binary_to_int = functions.udf(lambda x: int(x, 2), IntegerType())
data = data.withColumn("some_colum", binary_to_int("some_colum").alias('some_column_int'))