简体   繁体   English

如何在Spark SQL中从列表创建数据框?

[英]How to create dataframe from list in Spark SQL?

Spark version : 2.1 Spark版本:2.1

For example, in pyspark, i create a list 例如,在pyspark中,我创建一个列表

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: 那么如何从test_list创建一个数据框,其中数据框的类型如下所示:

DataFrame[words: array<string>]

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: 对Pushkr代码的略微调整(上图)给出:

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. 您应该使用Row对象列表([Row])来创建数据框。

from pyspark.sql import Row

spark.createDataFrame(list(map(lambda x: Row(words=x), test_list)))
   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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Spark Dataframe创建labledpoints以及如何将名称列表传递给VectorAssembler - Create labledpoints from Spark Dataframe & how to pass list of names to VectorAssembler Apache Spark:如何从DataFrame创建矩阵? - Apache Spark: How to create a matrix from a DataFrame? 如何使用来自 pandas dataframe 的 sql 文件创建 txt 文件夹列表 - How to create list of txt folders with sql files from pandas dataframe 如何从同时列出数据和架构的 JSON 文件创建 Spark-SQL dataframe - How to create a Spark-SQL dataframe from JSON file where data and schema are both listed 如何创建 Spark dataframe 以从 np.arrays 列表(由 RDKit 生成)提供给 sparks 随机森林实现? - How to create a Spark dataframe to feed to sparks random forest implementation from a list of np.arrays (generated by RDKit)? 从 pyspark.sql 中的列表创建数据框 - Create a dataframe from a list in pyspark.sql 如何从数据框创建列表? - How to create a list from the dataframe? 如何在火花 sql dataframe 中创建一个新列 map? - How to map a column to create a new column in spark sql dataframe? 如何从 dataframe 创建列表列表 - How to create a list of list from a dataframe 如何从 spark dataframe 的层次关系创建字典 - How to create dictionary from hierarchical relations from spark dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM