如何将spark df col转换成行

vyswwuz2  于 2021-05-17  发布在  Spark
关注(0)|答案(1)|浏览(261)

我尝试将spark df列转换成行,如下面的示例所示。

scala> df.show()
+----+--------------------+
|year|               String|
+----+--------------------+
|ina|List(Apple, 136, Train ...)...|
|inb|List(Orange, 4.36, car ...)...|
|ina|List(Apple,34, plane ...)...|
+----+--------------------+

并创建一个输出df,如下所示:

+----+-------------+-------------+
|year|key|String|
+----+-------------+-------------+
|ina|Apple       |136  |
|inb|Car         |4.36 |
|ina|Orange      |34   |

如何获得所需的输出?使用explode?
非常感谢!

pgvzfuti

pgvzfuti1#

如果将值列视为json:

val mappingSchema = MapType(StringType, FloatType)

originalDF.withColumn("map", from_json($"value", mappingSchema)).select($"year", explode($"map")).show()

给予:

+----+------+---------+
|year|   key|    value|
+----+------+---------+
|2020| Apple|1064.3667|
|2020|   Car|   1434.2|
|2020|Orange| 104.3667|
|2020| Plane|    145.2|
|2020| Apple|  1064.37|
|2020| Train|    134.2|
+----+------+---------+

如果需要,请重命名列

相关问题