在spark中为多个列名称添加前缀

时间:2019-04-04 09:25:29

标签: scala apache-spark dataframe

我编写了以下scala代码:

 val someDF = Seq(
 |   (8, "bat"),
 |   (64, "mouse"),
 |   (-27, "horse")
 | ).toDF("number", "word")

我的目的是将列名更改为“ pap_number”和“ pap_word”

我这样写:

val list = someDF.columns

然后

val result = list.foldLeft(df){(acc, names ) =>
 |   acc.withColumnRenamed(names, "pap_".concat(names)
 | }

但这对这个没有任何作用

2 个答案:

答案 0 :(得分:0)

您缺少一个括号并提供了错误的df。

scala> someDF.columns.foldLeft(someDF){ (acc, names) =>
 | acc.withColumnRenamed(names, "pap_".concat(names))
 | }
res2: org.apache.spark.sql.DataFrame = [pap_number: int, pap_word: string]

答案 1 :(得分:0)

另一种方法是使用此库来扩展spark函数: https://github.com/helkaroui/spark-tools

def morse_encode(text, code_table):
    return ' '.join(code_table.get(letter.upper()) for letter in text)