dataframe - 如何转换数组排列
问题描述
我有一个具有以下架构的数据框
root
|-- ColA: array
|--|--element: struct
|--|--|--id: string (nullable = true)
|--|--|--name: string (nullable = true)
预期数据框的架构将是,
root
|-- ColA: array
|--|--element: string (continsNull = true)
是否可以将结构数组转换为字符串数组?
任何帮助将非常感激。
谢谢你。
解决方案
假设您需要该字段name
,您可以像往常一样选择该数组列
(df
.withColumn('ColA', F.col('ColA.name'))
.printSchema()
)
# Input
+----------------+
| ColA|
+----------------+
|[{1, A}, {2, B}]|
+----------------+
# Output
+------+
| ColA|
+------+
|[A, B]|
+------+
推荐阅读
- java - 在Java中的子类中过滤掉父类中的记录
- .net-core - Team City 未能构建 Dot Net Core,但手动构建有效
- javascript - 在 Javascript 上本地获取 json 数据
- node.js - 使用 node.js 从 http 读取 MP3
- java - 有什么方法可以简化我编写的程序?(初级程序员)
- sql - 我需要帮助以特定方式完成更新
- php - 调用未定义的方法 mysqli_result::fetch()
- c++ - target_include_directories cmake 如何包含本地库 如何工作
- php - XAMPP 虚拟主机 - 错误 403 访问被拒绝
- azure-devops - 如何使用 Azure Devops Pipelines 构建包含 x86 和 ARM 构建的 appxpackage?