【发布时间】:2018-07-05 14:43:28
【问题描述】:
调用下面的代码时出错。这是在数据框中爆炸数组而不会丢失空值,但是在调用列时我收到错误消息 对象没有属性“_get_object_id”。需要以其他方式调用列的帮助。
from pyspark.sql import SparkSession
from pyspark.sql import DataFrame
from pyspark.sql import Row
from pyspark.sql.types import ArrayType
from pyspark.sql.functions import *
from functools import reduce
def explode_outer(df, columns_to_explode):
array_fields = dict([(field.name, field.dataType)
for field in df.schema.fields
if type(field.dataType) == ArrayType])
return reduce(lambda df_with_explode, column:
df_with_explode.withColumn(column, explode(
when(size(df_with_explode[column]) != 0, df_with_explode[column])
.otherwise(array(lit(None).cast(array_fields[column].elementType))))),
columns_to_explode, df)
做的时候:
cols = ['columname']
df_1 = df.select(explode_outer(df,cols))
错误:
AttributeError: 'DataFrame' object has no attribute '_get_object_id'
【问题讨论】:
-
是真实的代码还是你改编的?错误来自此代码还是原始代码?
-
这是错误的原始代码
标签: python python-3.x pyspark pyspark-sql