代码之家  ›  专栏  ›  技术社区  ›  Markus

SyntaxError:使用ArrayType创建数据帧时语法无效

  •  -2
  • Markus  · 技术社区  · 6 年前

    我想创建PySpark数据帧

    from pyspark.sql import SparkSession
    from pyspark.sql.types import *
    from pyspark.sql import Row
    
    spark = SparkSession \
        .builder \
        .appName("Test") \
        .master("local[4]") \
        .getOrCreate()
    
    schema = StructType([StructField('id', StringType()), \
                         StructField('timestamp',LongType()), \
                         StructField('coordinates',ArrayType())])
    rows = [Row(id="11", timestamp=1523975430000, coordinates = [41.5555, 2.1522])]
    
    df = spark.createDataFrame(rows, schema)
    

    SyntaxError: invalid syntax 旁边 lat . 我想 ArrayType 对象应该以不同的方式创建。 df ?

    更新:

    预期结果:

    id    timestamp       coordinates
    11    1523975430000    [41.5555, 2.1522]
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   hamza tuna    6 年前

    ArrayType需要元素的类型。尝试:

    schema = StructType([StructField('id', StringType()), \
                         StructField('timestamp',LongType()), \
                         StructField('coordinates',ArrayType(DoubleType()))])
    rows = [Row(id="11", timestamp=1523975430000, coordinates = [ 41.5555,  2.1522])]
    

    结果:

    +---+-------------+-----------------+
    | id|    timestamp|      coordinates|
    +---+-------------+-----------------+
    | 11|1523975430000|[41.5555, 2.1522]|
    +---+-------------+-----------------+