将具有数组列的数据框转换为长数据框

converting a dataframe with array columns to a long dataframe

我有列,它们是 spark DataFrame 中的数组。我想将数据帧转换为长数据帧,其中数组的每个元素都在一个新行中。 我希望它像下面的那样。

例如

spark.createDataFrame([(['abcd','cdf','dfg'],['123']),(['a'],['45','67',98'])],['s','d']).show()

您可以使用explode

df = spark.createDataFrame([(['abcd','cdf','dfg'],['123']),(['a'],['45','67','98'])],['s','d'])

df.createOrReplaceTempView("tbl")

spark.sql("select x, y from tbl lateral view explode(s) v1 as x lateral view explode(d) v2 as y").show()

+----+---+
|   x|  y|
+----+---+
|abcd|123|
| cdf|123|
| dfg|123|
|   a| 45|
|   a| 67|
|   a| 98|
+----+---+