首页 > 解决方案 > 如何转换数组排列

问题描述

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

root
|-- ColA: array
|--|--element: struct
|--|--|--id: string (nullable = true)
|--|--|--name: string (nullable = true)

预期数据框的架构将是,

root
|-- ColA: array
|--|--element: string (continsNull = true)

是否可以将结构数组转换为字符串数组?

任何帮助将非常感激。

谢谢你。

标签: dataframeapache-sparkpyspark

解决方案


假设您需要该字段name,您可以像往常一样选择该数组列

(df
    .withColumn('ColA', F.col('ColA.name'))
    .printSchema()
)

# Input
+----------------+
|            ColA|
+----------------+
|[{1, A}, {2, B}]|
+----------------+

# Output
+------+
|  ColA|
+------+
|[A, B]|
+------+

推荐阅读