How to create dataframe from list in Spark SQL?
Question:
Spark version : 2.1
For example, in pyspark, i create a list
test_list = [['Hello', 'world'], ['I', 'am', 'fine']]
then how to create a dataframe form the test_list, where the dataframe’s type is like below:
DataFrame[words: array<string>]
Answers:
You can create a RDD first from the input and then convert to dataframe from the constructed RDD
<code>
import sqlContext.implicits._
val testList = Array(Array("Hello", "world"), Array("I", "am", "fine"))
// CREATE RDD
val testListRDD = sc.parallelize(testList)
val flatTestListRDD = testListRDD.flatMap(entry => entry)
// COnvert RDD to DF
val testListDF = flatTestListRDD.toDF
testListDF.show
</code>
here is how –
from pyspark.sql.types import *
cSchema = StructType([StructField("WordList", ArrayType(StringType()))])
# notice extra square brackets around each element of list
test_list = [['Hello', 'world']], [['I', 'am', 'fine']]
df = spark.createDataFrame(test_list,schema=cSchema)
i had to work with multiple columns and types – the example below has one string column and one integer column. A slight adjustment to Pushkr’s code (above) gives:
from pyspark.sql.types import *
cSchema = StructType([StructField("Words", StringType())
,StructField("total", IntegerType())])
test_list = [['Hello', 1], ['I am fine', 3]]
df = spark.createDataFrame(test_list,schema=cSchema)
output:
df.show()
+---------+-----+
| Words|total|
+---------+-----+
| Hello| 1|
|I am fine| 3|
+---------+-----+
You should use list of Row objects([Row]) to create data frame.
from pyspark.sql import Row
spark.createDataFrame(list(map(lambda x: Row(words=x), test_list)))
Spark version : 2.1
For example, in pyspark, i create a list
test_list = [['Hello', 'world'], ['I', 'am', 'fine']]
then how to create a dataframe form the test_list, where the dataframe’s type is like below:
DataFrame[words: array<string>]
You can create a RDD first from the input and then convert to dataframe from the constructed RDD
<code>
import sqlContext.implicits._
val testList = Array(Array("Hello", "world"), Array("I", "am", "fine"))
// CREATE RDD
val testListRDD = sc.parallelize(testList)
val flatTestListRDD = testListRDD.flatMap(entry => entry)
// COnvert RDD to DF
val testListDF = flatTestListRDD.toDF
testListDF.show
</code>
here is how –
from pyspark.sql.types import *
cSchema = StructType([StructField("WordList", ArrayType(StringType()))])
# notice extra square brackets around each element of list
test_list = [['Hello', 'world']], [['I', 'am', 'fine']]
df = spark.createDataFrame(test_list,schema=cSchema)
i had to work with multiple columns and types – the example below has one string column and one integer column. A slight adjustment to Pushkr’s code (above) gives:
from pyspark.sql.types import *
cSchema = StructType([StructField("Words", StringType())
,StructField("total", IntegerType())])
test_list = [['Hello', 1], ['I am fine', 3]]
df = spark.createDataFrame(test_list,schema=cSchema)
output:
df.show()
+---------+-----+
| Words|total|
+---------+-----+
| Hello| 1|
|I am fine| 3|
+---------+-----+
You should use list of Row objects([Row]) to create data frame.
from pyspark.sql import Row
spark.createDataFrame(list(map(lambda x: Row(words=x), test_list)))