【发布时间】:2020-05-09 01:06:54
【问题描述】:
如何将多列对分解为多行?
我有一个包含以下内容的数据框
client, type, address, type_2, address_2
abc, home, 123 Street, business, 456 Street
我想要一个最终的数据框,如下所示
client, type, address
abc, home, 123 Street
abc, business, 456 Street
我尝试使用下面的代码,但它返回了 4 条记录,而不是我想要的两条记录
df
.withColumn("type", explode(array("type", "type_2")))
.withColumn("address", explode(array("address", "address_2")))
我可以用两个单独的数据框执行此操作并执行联合,但我想看看是否有另一种方法可以在单个数据框中执行此操作
谢谢
【问题讨论】:
-
请检查我提供的其他解决方案。
标签: scala apache-spark