val tvalues: Array[Double] = Array(1.866393526974307, 2.864048126935307, 4.032486069215076, 7.876169953355888, 4.875333799256043, 14.316322626848278)
val pvalues: Array[Double] = Array(0.064020056478447, 0.004808399479386827, 8.914865448939047E-5, 7.489564524121306E-13, 2.8363794106756046E-6, 0.0)
I have two Arrays as above, i need to build a DataFrame from this Arrays like the following,
Tvalues Pvalues
1.866393526974307 0.064020056478447
2.864048126935307 0.004808399479386827
...... .....
As of now i am trying with StringBuilder
in Scala. which doesnt go as expected. help me on this please.
Try for instance
val df = sc.parallelize(tpvalues zip pvalues).toDF("Tvalues","Pvalues")
and thus
scala> df.show
+------------------+--------------------+
| Tvalues| Pvalues|
+------------------+--------------------+
| 1.866393526974307| 0.064020056478447|
| 2.864048126935307|0.004808399479386827|
| 4.032486069215076|8.914865448939047E-5|
| 7.876169953355888|7.489564524121306...|
| 4.875333799256043|2.836379410675604...|
|14.316322626848278| 0.0|
+------------------+--------------------+
Using parallelize
we obtain an RDD
of tuples -- the first element from the first array, the second element from the other array --, which is transformed into a dataframe of rows, one row for each tuple.
Update
For dataframe'ing multiple arrays (all with the same size), for instance 4 arrays, consider
case class Row(i: Double, j: Double, k: Double, m: Double)
val xs = Array(arr1, arr2, arr3, arr4).transpose
val rdd = sc.parallelize(xs).map(ys => Row(ys(0), ys(1), ys(2), ys(3))
val df = rdd.toDF("i","j","k","m")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With