Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add null values in an array in spark scala

val data = Array(-999.9,-0.5, -0.3, 0.0, 0.2, 999.9)
 val dataFrame = sqlContext.createDataFrame(data.map(Tuple1.apply)).toDF("features")

I want to introduce the null entry in the above array. I tried below but it didn't work.

val data = Array(-999.9,-0.5, -0.3, 0.0, 0.2, 999.9, null)
like image 912
user6200992 Avatar asked Oct 19 '25 04:10

user6200992


1 Answers

You need to make the array of type Option and the null will be None:

val data = Array(Some(-999.9),Some(-0.5), Some(-0.3), Some(0.0), Some(0.2), Some(999.9),None)
// data: Array[Option[Double]] = Array(Some(-999.9), Some(-0.5), Some(-0.3), Some(0.0), Some(0.2), Some(999.9), None)

val dataFrame = spark.createDataFrame(data.map(Tuple1.apply)).toDF("features")
// dataFrame: org.apache.spark.sql.DataFrame = [features: double]

dataFrame.show    
+--------+
|features|
+--------+
|  -999.9|
|    -0.5|
|    -0.3|
|     0.0|
|     0.2|
|   999.9|
|    null|
+--------+
like image 182
Psidom Avatar answered Oct 21 '25 19:10

Psidom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!