PySpark:DataFrame - 将Struct转换为数组

时间:2017-12-03 08:24:05

标签: apache-spark pyspark apache-spark-sql 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)

我需要将其更改为:

root
 |-- index: long (nullable = true)
 |-- text: string (nullable = true)
 |-- topicDistribution: array (nullable = true)
 |    |--  element: double (containsNull = true)
 |-- wiki_index: string (nullable = true)

请问我怎么能这样做?

非常感谢。

1 个答案:

答案 0 :(得分:4)

我认为你正在寻找

df.withColumn("topicDistribution", col("topicDistribution").getField("values"))