我從 Spark 和 Scala 開始,我想知道我該怎么做:
我有一個資料框,其中有一列具有這些不同的值(R1,R2,M1,M2,I1,I2),我想映射這些值并創建一個新列,其值取決于另一列中映射的值。例如,我想映射第一列并獲得類似第二列的內容
R1 it starts with R
R1 it starts with R
R2 it starts with R
M1 it starts with M
M2 it starts with M
I1 it starts with I
謝謝
uj5u.com熱心網友回復:
import org.apache.spark.sql.functions._
import spark.implicits._
val substring = udf((str: String) => "Please, first use search ".concat(str.substring(0,1)))
val source = Seq("R1", "R2", "M1", "M2", "I1", "I2")
.toDF("col1")
.withColumn("col2", substring(col("col1")))
source.show(false)
// ---- --------------------------
// |col1|col2 |
// ---- --------------------------
// |R1 |Please, first use search R|
// |R2 |Please, first use search R|
// |M1 |Please, first use search M|
// |M2 |Please, first use search M|
// |I1 |Please, first use search I|
// |I2 |Please, first use search I|
// ---- --------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/493066.html
上一篇:Spark-合并兩列陣列結構型別
下一篇:React:如何在`contentEditable`div上使用`InputEvent`引數捕獲`onInput`事件?
