PySpark:DataFrame-将结构转换为数组

Ipp*_*pon 0 apache-spark apache-spark-sql pyspark spark-dataframe

我有以下结构的数据框:

root
 |-- index: long (nullable = true)
 |-- text: string (nullable = true)
 |-- topicDistribution: struct (nullable = true)
 |    |-- type: long (nullable = true)
 |    |-- values: array (nullable = true)
 |    |    |-- element: double (containsNull = true)
 |-- wiki_index: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)

我需要将其更改为:

root
 |-- index: long (nullable = true)
 |-- text: string (nullable = true)
 |-- topicDistribution: array (nullable = true)
 |    |--  element: double (containsNull = true)
 |-- wiki_index: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)

请问我该怎么做?

非常感谢。

ayp*_*lam 5

我想你在找

df.withColumn("topicDistribution", col("topicDistribution").getField("values"))
Run Code Online (Sandbox Code Playgroud)