scala - 连接多列的更好方法?
问题描述
我有 30 列。26 个列名是字母的名称。我想把这 26 列做成一列作为一个字符串。
price dateCreate volume country A B C D E ..... Z
19 20190501 25 US 1 2 5 6 19 30
49 20190502 30 US 5 4 5 0 34 50
我要这个:
price dateCreate volume country new_col
19 20190501 25 US "1,2,5,6,19,....30"
49 20190502 30 US "5,4,5,0,34,50"
我知道我可以做这样的事情:
df.withColumn("new_col", concat($"A", $"B", ...$"Z"))
但是,将来遇到这个问题时,我想知道如何更轻松地连接许多列。有办法吗?
解决方案
只需将以下内容应用于要连接的任意数量的列
val df= Seq((19,20190501,24, "US", 1 , 2, 5, 6, 19 ),(49,20190502,30, "US", 5 , 4, 5, 0, 34 )).
toDF("price", "dataCreate", "volume", "country", "A","B","C","D","E")
val exprs = df.columns.drop(4).map(col _)
df.select($"price", $"dataCreate", $"volume", $"country", concat_ws(",",
array(exprs: _*)).as("new_col"))
+-----+----------+------+-------+----------+
|price|dataCreate|volume|country| new_col|
+-----+----------+------+-------+----------+
| 19| 20190501| 24| US|1,2,5,6,19|
| 49| 20190502| 30| US|5,4,5,0,34|
+-----+----------+------+-------+----------+
为了完整起见,这里是 pyspark 等价物
import pyspark.sql.functions as F
df= spark.createDataFrame([[19,20190501,24, "US", 1 , 2, 5, 6, 19 ],[49,20190502,30, "US", 5 , 4, 5, 0, 34 ]],
["price", "dataCreate", "volume", "country", "A","B","C","D","E"])
exprs = [col for col in df.columns[4:]]
df.select("price","dataCreate", "volume", "country", F.concat_ws(",",F.array(*exprs)).alias("new_col"))
推荐阅读
- javascript - Bad rendering for borders of circles on a game in Canvas
- c# - 如何在 x64 位 Windows 窗体应用程序中使用 OpenWebKitSharp 库
- sql-server - 在 switch 语句 SQL 中包装 If 语句
- javascript - 在现有应用程序中反应组件
- c - 从字符串数组中的字符串中删除空格
- sql - 如何以oracle形式更改变量的值
- reactjs - 运行带有样式组件的下一个 js 应用程序时出现内部服务器错误
- azure - Azure WebJob vNet 集成
- c# - 如何将 polygon2Dcollider 点存储在数组或列表中
- azerothcore - 如何在 MySQL 5.7 中使用 AzerothCore