1

I have a DataFrame with three columns; How can I convert it to a nested array in Scala - Array[Array[Double]]?

I am getting Array[(Double, Double)] when i try to map:

val x= dataframe.select("time","sex")
  .collect()
  .map(x=>x(0).toString.toDouble ,x(1).toString.toDouble)

1 Answer 1

1

That is because your map is returning a tuple. since you needed an Array you need to return an Array as shown below.

val df = dataframe.collect().map(x => Array(x.getDouble(0), x.getDouble(1)))

to access the first item (nested Array) in the result use df.head or df(0)

EDIT:

to have the dataset in columnar fashion Array[Array[Double]] where the each column has its own array.

df.foldLeft((Array[Double](),Array[Double]())) {
  case ((col1,col2),(x,y)) => (col1 :+ x) -> (col2 :+ y)
} match {
    case (arr1,arr2) => Array(arr1,arr2)
}
Sign up to request clarification or add additional context in comments.

10 Comments

How can we access first array from it?
@BejjamDEEPTHIPRIYA can you please elaborate your request?
From the above command we will getv array[array[double]] how can we access first array ???
Hi actually I'm looking for nestedarray[array[array1,array2]] array 1 is one Column of dataframe and array 2 is another column and the solution u provided gives me arrays of each row .do u have any suggestion for my qstion if so please let me know
@Deep so you need Array[Array[Double],Array[Double]]. ie an Array with two arrays for each column (ie a column oriented storage)
|

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.