【问题标题】:JSON List conversionJSON列表转换
【发布时间】:2020-06-18 12:37:31
【问题描述】:

我的 JSON 值(元组)列表如下所示:

testJson = [('{"drivernumber":1, "speed" : ["30.5", "40", "50", "25.25"]}',),
            ('{"drivernumber":2, "speed" : ["25.25", "10.11", "11", "50"]}',),
            ('{"drivernumber":3, "speed" : ["40", "50", "80", "42"]}',)
           ]

我创建了以下数据结构:

from pyspark.sql.types import StructType, StructField, IntegerType, ArrayType, StringType
readSchema = StructType([
                   StructField("drivernumber", IntegerType(), True), 
                   StructField("speed", StringType(FloatType(), True), True)])

然后创建了一个DataFrame:

df = (spark.read.schema(readSchema).json(sc.parallelize(testJson)))
display(df)

最终,我需要得到以下输出,但目前,我的 DF(在上述步骤之后)只有 NULLS,我不知道为什么。任何线索或提示将不胜感激。谢谢你:)

speed  drivercount
50          3
40          2
25.25       2
11          1
....        ....

【问题讨论】:

  • 您是否有理由需要单元素元组列表?
  • 嗨@JohnGordon - 只是数据由代理以这种格式传递。

标签: python json python-3.x dataframe python-requests


【解决方案1】:

您不必为它定义架构,只需使用即可

df = spark.read.json(sc.parallelize(testJson))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 2021-08-27
    • 2018-12-13
    • 1970-01-01
    • 2018-06-22
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多