0

For this code :

var p = result.select("finalFeatures").head.toSeq.toArray

The results looks like this :

p: Array[Any] = Array([3.0,6.0,-0.7876947819954485,-0.21757635218517163,0.9731844373162398,-0.6641741696340382,-0.6860072219935377,-0.2990737363481845,-0.7075863760365155,0.8188108975549018,-0.8468559840943759,-0.04349947247406488,-0.45236764452589984,1.0333959313820456,0.609756607087835,-0.7106619551471779,-0.7750330808435969,-0.08097610412658443,-0.45338437108038904,-0.2952869863393396,-0.30959772365257004,0.6988768123463287,0.17049117199049213,3.2674649019757385,-0.8333373234944124,1.8462942520757128,-0.49441222531240125,-0.44187299748074166,-0.300810826687287])

I need this to be Array[Double] How would i do this ?

2
  • result is dataframe? Commented Apr 3, 2019 at 12:16
  • Did you try .asInstanceOf[Array[Double]] ? Commented Apr 3, 2019 at 14:32

2 Answers 2

2

You can convert an Array of Any to Double as follows:

 val pAsDouble = p.map(_.toString.toDouble)
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you have the following data:

val df = Seq(Array("2.3", "2.0", "5")).toDF("finalFeatures")
df.show

The output for the previous command will be:

+-------------+
|finalFeatures|
+-------------+
|[2.3, 2.0, 5]|
+-------------+

df.schema will print org.apache.spark.sql.types.StructType = StructType(StructField(finalFeatures,ArrayType(StringType,true),true)) to cast the column into double array you can do:

val doubleSeq = df.select($"finalFeatures".cast("array<double>")).head.get(0).asInstanceOf[Seq[Double]]

And doubleSeq.foreach(println _) should have the next output:

2.3
2.0
5.0

6 Comments

print schema gives this root |-- finalFeatures: vector (nullable = true)
gives error org.apache.spark.sql.AnalysisException: cannot resolve 'finalFeatures' due to data type mismatch: cannot cast vector to array<double>;;
df.select($"finalFeatures".cast("array<double>")).printSchema should print root |-- finalFeatures: array (nullable = true) | |-- element: double (containsNull = true)
hi @Leothorn could post the schema of result dataframe as well?
hi @Alexandros . No resultant df . org.apache.spark.sql.AnalysisException: cannot resolve 'finalFeatures' due to data type mismatch: cannot cast vector to array<double>;; 'Project [unresolvedalias(cast(finalFeatures#9911 as array<double>), None)]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.