scala - 在数据框 (Spark) 中选择列时在其间添加一个空列
问题描述
我正在尝试在数据框选择语句的两列之间添加一个空列。
使用该withColumn
功能,我只能追加作为结束列,但我需要中间的空列(第 3 列和第 6 列),如下所示。
val product1 = product.select("_c1","_c2"," ","_c4", "_c5", "_c5", " ", "c6")
我尝试withColumn
在select
语句中间使用,如下所示,它给出了错误:
val product1 = product.select("_c1","_c2",product.withColumn("NewCol",lit(None).cast("string")),"_c4", "_c5", "_c5", " ", "c6")
>error: overloaded method value select with alternatives:
(col: String,cols: String*)org.apache.spark.sql.DataFrame <and>
(cols: org.apache.spark.sql.Column*)org.apache.spark.sql.DataFrame
cannot be applied to (String, String, String, String, String, String, String, String, org.apache.spark.sql.DataFrame, String)
如果有任何建议,请告诉我。谢谢
解决方案
为了在数据框中选择列,可以使用字符串(列名)或列(Column
类型)作为输入。从文档中:
def select(col: String, cols: String*): DataFrame Selects a set of columns.
def select(cols: Column*): DataFrame Selects a set of column based expressions.
但是,这些不能混用。在这种情况下,请使用select
withColumn
类型。要获取特定名称的列,请使用col
函数或$
(在导入 spark 隐式之后)。
val spark = SparkSession()....
import spark.implicits._
val product1 = product.select($"_c1", $"_c2", lit(" ").as("newCol1"), $"_c4", $"_c5", $"_c5", lit(" ").as("newCol2"), $"c6")
推荐阅读
- arrays - 在 C 中迭代 MDA
- c# - 如何将相等过滤器应用于用户请求?
- json - 尝试使用 Play Json 添加 json 解析时“无法访问”隐式 val
- ruby-on-rails - 如何在会话中存储和使用多个 cart_id
- git - git add 没有添加整个代码库,代码现在被覆盖
- php - 当日期时间列<现在如何更新MySQL列/行?
- heroku - Heroku 上的 Celery beat(Redbeat):日志未显示
- list - (TI_84 Plus) 将变量与“if”语句的数字列表进行比较
- mysql - 在获得结果之前单独处理mysql“group by”中的每个项目?
- c# - 具有多对多关系的 Remove() 不起作用