【问题标题】:PySpark flatten dataframe having some columns as array of nested structsPySpark 展平数据框,其中一些列作为嵌套结构的数组
【发布时间】:2020-12-07 22:17:42
【问题描述】:

我有一个具有以下架构的数据框:

root
|-- first_name: string
|-- last_name: string
|-- details: array
|    |-- element: struct
|    |    |-- university: string
|    |    |-- subjects: struct
|    |    |    |-- subject1: string
|    |    |    |-- subject2: string
|-- grades: array
|    |-- element: struct
|    |    |-- sem1: string
|    |    |-- sem2: struct

我想将它展平为以下架构,这样我就不再有任何结构,而是将数组作为独立的列。

root
|-- first_name: string
|-- last_name: string
|-- details.university: array
     |-- element: string
|-- details.subjects.subject1: array
     |-- element: string
|-- details.subjects.subject2: array
     |-- element: string
|-- grades.sem1: array
     |-- element: string
|-- grades.sem2: array
     |-- element: string

我正在努力做同样的事情,我非常感谢一些帮助。谢谢!

【问题讨论】:

标签: python json apache-spark pyspark flatten


【解决方案1】:

只需选择所需的列

df.select('first_name','last_name','details.university','details.subjects.subject1',
          'details.subjects.subject2','grades.sem1','grades.sem2')

【讨论】:

  • 这里的数据类型是结构数组,而不仅仅是结构,所以我认为这不会在这里工作。
  • 试试先生.. 它会创建数组
猜你喜欢
  • 2023-03-17
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 2021-10-11
  • 2020-10-04
  • 2020-09-27
  • 2018-07-24
相关资源
最近更新 更多