StructType不能接受pyspark中的对象浮点数

时间:2018-03-24 12:01:42

标签: python pyspark tuples

为什么这样做很好

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'>

1 个答案:

答案 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|
+-----+