Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert column values from string to decimal?

I'm having a dataframe which contains a really big integer value, example:

42306810747081022358

When I've tried to convert it to long it was working in the Java but not under the spark envrironment, I was getting

   NumberFormatException: For input string("42306810747081022358")

Then I tried to convert it too Decimal (BigDecimal) value. Again, easily can do it in Java, but in Spark: dframe.withColumn("c_number",col("c_a").cast(new DecimalType()));

This way I don't get any exceptions, however I can see that all result values are null.

I also tried to use UDF for this purpose but get the same results:

UDF1 cTransformer = new UDF1<String, BigDecimal>() {
        @Override
        public BigDecimal call(String aString) throws Exception {
            return new BigDecimal(aString);
        }
    };
sqlContext.udf().register("cTransformer", cTransformer, new DecimalType());
dframe = dframe.withColumn("c_number", callUDF("cTransformer", dframe.col("c_a"))); 

And here again all I'm getting is a column with all zeroes.

How should I proceed?

like image 483
Igor Kustov Avatar asked Oct 24 '16 18:10

Igor Kustov


People also ask

How do you convert columns to decimals?

Use the CAST() function to convert an integer to a DECIMAL data type. This function takes an expression or a column name as the argument, followed by the keyword AS and the new data type. In our example, we converted an integer (12) to a decimal value (12.00).

How do you convert a string to a decimal?

Converting a string to a decimal value or decimal equivalent can be done using the Decimal. TryParse() method. It converts the string representation of a number to its decimal equivalent.

Can you convert varchar to decimal in SQL?

To convert a varchar type to a numeric type, change the target type as numeric or BIGNUMERIC as shown in the example below: SELECT CAST('344' AS NUMERIC) AS NUMERIC; SELECT CAST('344' AS BIGNUMERIC) AS big_numeric; The queries above should return the specified value converted to numeric and big numeric.

How do you convert a column to a decimal in Pyspark?

In order to typecast an integer to decimal in pyspark we will be using cast() function with DecimalType() as argument, To typecast integer to float in pyspark we will be using cast() function with FloatType() as argument.


1 Answers

Try:

dframe.withColumn("c_number", dframe.col("c_a").cast("decimal(38,0)"))
like image 58
user6022341 Avatar answered Oct 13 '22 14:10

user6022341