将pyspark中的字符串数组的一列转换为一列中的多行

时间:2019-12-16 23:22:08

标签: hive pyspark

我想将带有列的表转换为databricks pyspark上的字符串数组。

我的桌子:

 id         values  (array<string>)
 rgf        ['vwervfrev', 'fweccf', 'tuyhert']
 rty        ['evvverws', 'ilonmunt', 'cedcrhb']

我需要什么:

 id         values
 rdf        'vwervfrev'
 rdf        'fweccf'
 rdf        'tuyhert'
 rty        'evvverws'
 rty        'ilonmunt'
 rty        'cedcrhb'

我不确定如何进行转换?

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用爆炸功能来做到这一点:

from pyspark.sql.functions import explode, col

new_df = df.withColumn("values", explode(col("values")))
new_df.show()

https://spark.apache.org/docs/latest/api/python/_modules/pyspark/sql/functions.html#explode