【问题标题】:PYSPARK:- Exploding the array in dataframe without loosing null values:'DataFrame' object has no attribute '_get_object_id'PYSPARK:-在数据框中爆炸数组而不丢失空值:'DataFrame'对象没有属性'_get_object_id'
【发布时间】: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


【解决方案1】:

由于在从嵌套数据中获取列时出现 Key 错误,因此已完成。处理该代码后工作正常......更新问题也以供其他人正确使用。 谢谢

【讨论】:

    猜你喜欢
    • 2019-12-13
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多