为什么这样做很好
from pyspark.sql.types import *
l=[("foo",83.33)]
schema = StructType([
StructField("type", StringType(), True),
StructField("value", DoubleType(), True)])
df= spark.createDataFrame(l,schema)
这个
l=[(83.33)]
schema = StructType([
StructField("value", DoubleType(), True)])
df= spark.createDataFrame(l,schema)
给我一个错误
StructType can not accept object 83.33 in type <class 'float'>
答案 0 :(得分:3)
使用单元素元组时,需要使用尾随逗号。请检查此TupleSyntax
>>> l=[(83.33,)] //note the comma (,)
>>> schema = StructType([
... StructField("value", DoubleType(), True)])
>>> df= spark.createDataFrame(l,schema)
>>> df.show()
+-----+
|value|
+-----+
|83.33|
+-----+