这个问题在这里已有答案:
这种情况非常简单,我需要使用以下代码将python列表转换为数据框
from pyspark.sql.types import StructType
from pyspark.sql.types import StructField
from pyspark.sql.types import StringType, IntegerType
schema = StructType([StructField("value", IntegerType(), True)])
my_list = [1, 2, 3, 4]
rdd = sc.parallelize(my_list)
df = sqlContext.createDataFrame(rdd, schema)
df.show()
它失败并出现以下错误:
raise TypeError("StructType can not accept object %r in type %s" % (obj, type(obj)))
TypeError: StructType can not accept object 1 in type <class 'int'>
2 回答
此解决方案也是一种使用较少代码的方法,避免了对RDD的序列化,并且可能更容易理解:
注意:关于命名变量
list
:术语list
是Python内置函数,因此,强烈建议我们避免使用内置名称作为变量的名称/标签,因为我们最终会覆盖list()
函数之类的东西 . 当快速和肮脏的原型,许多人使用类似的东西:mylist
.请参阅以下代码:
df