“type”对象不是可下标的映射数据帧

2024-09-28 17:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图构造一个复杂的模式结构(StructType),以便在加载JSON文件时映射到数据帧,但是我遇到了一个错误

'type' object is not subscriptable

下面是我正在尝试的一段代码:

from pyspark.sql.types import StructField, StringType, IntegerType, StructType

baseshema = StructType[StructField('ID Number', StringType(), True),
              StructField('Entity Name', StringType(), True),
              StructField('Phone Number', StringType(), True),
            StructField('Address',StructType[StructField('Building', StringType(), True),
              StructField('Street', StringType(), True),
              StructField('City', StringType(), True),
              StructField('State', StringType(), True),
              StructField('Postcode', StringType(), True)]),
             StructField('Location',StructType[StructField('Latitude', StringType(), True),
              StructField('Longitude', StringType(), True),
              StructField('Location', StringType(), True)])]

Tags: 文件数据jsontruenumberobjectistype
1条回答
网友
1楼 · 发布于 2024-09-28 17:18:48

必须使用括号来调用构造函数。此外,init使用一个参数,因此必须将字段包装在列表中。应该是这样的:

baseshema = StructType([StructField('ID Number', StringType(), True),
                        StructField('Entity Name', StringType(), True),
                        StructField('Phone Number', StringType(), True),
                        StructField('Address',StructType([
                            StructField('Building', StringType(), True),
                            StructField('Street', StringType(), True),
                            StructField('City', StringType(), True),
                            StructField('State', StringType(), True),
                            StructField('Postcode', StringType(), True)])),
                        StructField('Location',StructType([
                            StructField('Latitude', StringType(), True),
                            StructField('Longitude', StringType(), True),
                            StructField('Location', StringType(), True)]))])

相关问题 更多 >