Sar*_*rah 3 python apache-spark apache-spark-sql pyspark
我有一个看起来像这样的数据框:
|-- name: string (nullable = true)
|-- age: string (nullable = true)
|-- job: string (nullable = true)
|-- hobbies: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- favorite: string (nullable = true)
| | |-- non-favorite: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取以下信息:
['favorite', 'non-favorite']
Run Code Online (Sandbox Code Playgroud)
然而,我发现的唯一最接近的解决方案是将爆炸函数与 一起使用withColumn,但它是基于我已经知道元素名称的假设。但我想要做的是,在不知道元素名称的情况下,我只想获取仅包含列名称的元素名称,在本例中为“爱好”。有没有一种好方法来获取任何给定列中的所有元素名称?
对于具有此模式的给定数据框:
df.printSchema()
root
|-- hobbies: array (nullable = false)
| |-- element: struct (containsNull = false)
| | |-- favorite: string (nullable = false)
| | |-- non-favorite: string (nullable = false)
Run Code Online (Sandbox Code Playgroud)
您可以选择结构体的字段名称:
struct_fields = df.schema['hobbies'].dataType.elementType.fieldNames()
# output: ['favorite', 'non-favorite']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4363 次 |
| 最近记录: |