将Pyspark Dataframe列从数组转换为新列

Jul*_*mez 4 dataframe pyspark

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

root
 |-- Id: string (nullable = true)
 |-- Q: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- pr: string (nullable = true)
 |    |    |-- qt: double (nullable = true)
Run Code Online (Sandbox Code Playgroud)

类似于:

 +----+--------------------- ... --+
 | Id |           Q                |
 +----+---------------------- ... -+
 | 001| [ [pr1,1.9], [pr3,2.0]...] |
 | 002| [ [pr2,1.0], [pr9,3.9]...] |
 | 003| [ [pr2,9.0], ...         ] |
  ...
Run Code Online (Sandbox Code Playgroud)

我想将Q数组转换为列(名称pr值qt)。我也想通过合并(添加)相同的列来避免重复的列。

 +----+-----+-----+------+ ... ----+
 | Id | pr1 | pr2 | pr3  | ... prn |
 +----+-----+-----+------+ ... ----+
 | 001| 1.9 | 0.0 | 2.0  | ...     |
 | 002| 0.0 | 1.0 | 0    | ...     |
 | 003| 0.0 | 9.0 | ...  | ...     |
  ...
Run Code Online (Sandbox Code Playgroud)

我如何执行此转换?提前Thakyou!朱利安。

ags*_*s29 8

您可以结合使用explodepivot

import pyspark.sql.functions as F

# explode to get "long" format
df=df.withColumn('exploded', F.explode('Q'))

# get the name and the name in separate columns
df=df.withColumn('name', F.col('exploded').getItem(0))
df=df.withColumn('value', F.col('exploded').getItem(1))

# now pivot
df.groupby('Id').pivot('name').agg(F.max('value')).na.fill(0)
Run Code Online (Sandbox Code Playgroud)