StructType 不能接受 pyspark 中的对象浮动

Yak*_*kov 2 python tuples pyspark

为什么这工作得很好

from pyspark.sql.types import *
l=[("foo",83.33)]
schema = StructType([
   StructField("type", StringType(), True),
   StructField("value", DoubleType(), True)])
df= spark.createDataFrame(l,schema)
Run Code Online (Sandbox Code Playgroud)

和这个

 l=[(83.33)]
    schema = StructType([
    StructField("value", DoubleType(), True)])
    df= spark.createDataFrame(l,schema)
Run Code Online (Sandbox Code Playgroud)

给我一个错误

StructType can not accept object 83.33 in type <class 'float'>
Run Code Online (Sandbox Code Playgroud)

Bal*_*ala 5

使用单元素元组时,需要尾随逗号。检查这个元组语法

>>> l=[(83.33,)]           //note the comma (,)
>>> schema = StructType([
...     StructField("value", DoubleType(), True)])
>>> df= spark.createDataFrame(l,schema)
>>> df.show()
+-----+
|value|
+-----+
|83.33|
+-----+
Run Code Online (Sandbox Code Playgroud)