Convert List into dataframe spark scala

33,827

Solution 1

List("a","b","c","d") represents a record with one field and so the resultset displays one element in each row.

To get the expected output, the row should have four fields/elements in it. So, we wrap around the list as List(("a","b","c","d")) which represents one row, with four fields. In a similar fashion a list with two rows goes as List(("a1","b1","c1","d1"),("a2","b2","c2","d2"))

scala> val list = sc.parallelize(List(("a", "b", "c", "d"))).toDF()
list: org.apache.spark.sql.DataFrame = [_1: string, _2: string, _3: string, _4: string]

scala> list.show
+---+---+---+---+
| _1| _2| _3| _4|
+---+---+---+---+
|  a|  b|  c|  d|
+---+---+---+---+


scala> val list = sc.parallelize(List(("a1","b1","c1","d1"),("a2","b2","c2","d2"))).toDF
list: org.apache.spark.sql.DataFrame = [_1: string, _2: string, _3: string, _4: string]

scala> list.show
+---+---+---+---+
| _1| _2| _3| _4|
+---+---+---+---+
| a1| b1| c1| d1|
| a2| b2| c2| d2|
+---+---+---+---+

Solution 2

In order to use toDF we have to import

import spark.sqlContext.implicits._

Please refer below code

val spark = SparkSession.
builder.master("local[*]")
  .appName("Simple Application")
.getOrCreate()

import spark.sqlContext.implicits._

val lstData = List(List("vks",30),List("harry",30))
val mapLst = lstData.map{case List(a:String,b:Int) => (a,b)}
val lstToDf = spark.sparkContext.parallelize(mapLst).toDF("name","age")
lstToDf.show

val llist = Seq(("bob", "2015-01-13", 4), ("alice", "2015-04- 23",10)).toDF("name","date","duration")
llist.show

Solution 3

this will do:

val data = List(("Value1", "Cvalue1", 123, 2254, 22),("Value1", "Cvalue2", 124, 2255, 23));
val df = spark.sparkContext.parallelize(data).toDF("Col1", "Col2", "Expend1", "Expend2","Expend3");
val cols=Array("Expend1","Expend2","Expend3");
val df1=df
        .withColumn("keys",lit(cols))
        .withColumn("values",array($"Expend1",$"Expend2",$"Expend3"))
        .select($"col1",$"col2",explode_outer(map_from_arrays($"keys", $"values")))
        .show(false)
Share:
33,827
senthil kumar p
Author by

senthil kumar p

Updated on August 14, 2020

Comments

  • senthil kumar p
    senthil kumar p over 3 years

    I have a list with more than 30 strings. how to convert list into dataframe . what i tried:

    eg

    Val list=List("a","b","v","b").toDS().toDF()
    
    Output :
    
    
    +-------+
    |  value|
    +-------+
    |a      |
    |b      |
    |v      |
    |b      |
    +-------+
    
    
    Expected Output is 
    
    
      +---+---+---+---+
    | _1| _2| _3| _4|
    +---+---+---+---+
    |  a|  b|  v|  a|
    +---+---+---+---+
    

    any help on this .

  • aName
    aName almost 5 years
    I'm facing an issue using the toDF, I found the the result of calling parallelize is RDD[T] which doesn't have the toDF method
  • Carlos
    Carlos almost 5 years
    You are probably forgetting this: import spark.sqlContext.implicits._
  • Dwarrior
    Dwarrior over 4 years
    can I directly use mapLst.toDF("name","age") in spark 2.x instead of converting list to RDD using parallelize and then doing toDF? I believe toDF will directly take care of parallelizing the dataset.
  • user1896796
    user1896796 over 3 years
    How do I declare this List(("a1","b1","c1","d1"),("a2","b2","c2","d2")). Is it a list[List[String]] ?? I need to take it dynamically from a loop on a table query output.