将UDF应用于Spark Dataframe中的多个列 [英] Apply UDF to multiple columns in Spark Dataframe
本文介绍了将UDF应用于Spark Dataframe中的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的数据框
I have a dataframe which looks like below
| id| age| rbc| bgr| dm|cad|appet| pe|ane|classification|
+---+----+------+-----+---+---+-----+---+---+--------------+
| 3|48.0|normal|117.0| no| no| poor|yes|yes| ckd|
....
....
....
我写了一个UDF将分类yes, no, poor, normal
转换为二进制0s
和1s
I have written an UDF to convert categorical yes, no, poor, normal
into binary 0s
and 1s
def stringToBinary(stringValue: String): Int = {
stringValue match {
case "yes" => return 1
case "no" => return 0
case "present" => return 1
case "notpresent" => return 0
case "normal" => return 1
case "abnormal" => return 0
}
}
val stringToBinaryUDF = udf(stringToBinary _)
我将其应用于数据框,如下所示
I am applying this to the dataframe as follows
val newCol = stringToBinaryUDF.apply(col("pc")) //creates the new column with formatted value
val refined1 = noZeroDF.withColumn("dm", newCol) //adds the new column to original
如何将多个列传递到UDF中,这样我就不必为其他分类列重复我自己了?
推荐答案
udf
函数.
给出dataframe
为
+---+----+------+-----+---+---+-----+---+---+--------------+
|id |age |rbc |bgr |dm |cad|appet|pe |ane|classification|
+---+----+------+-----+---+---+-----+---+---+--------------+
|3 |48.0|normal|117.0|no |no |poor |yes|yes|ckd |
+---+----+------+-----+---+---+-----+---+---+--------------+
您可以使用when
函数来实现您的要求
You can achieve your requirement with when
function as
import org.apache.spark.sql.functions._
def applyFunction(column : Column) = when(column === "yes" || column === "present" || column === "normal", lit(1))
.otherwise(when(column === "no" || column === "notpresent" || column === "abnormal", lit(0)).otherwise(column))
df.withColumn("dm", applyFunction(col("dm")))
.withColumn("cad", applyFunction(col("cad")))
.withColumn("rbc", applyFunction(col("rbc")))
.withColumn("pe", applyFunction(col("pe")))
.withColumn("ane", applyFunction(col("ane")))
.show(false)
结果是
+---+----+---+-----+---+---+-----+---+---+--------------+
|id |age |rbc|bgr |dm |cad|appet|pe |ane|classification|
+---+----+---+-----+---+---+-----+---+---+--------------+
|3 |48.0|1 |117.0|0 |0 |poor |1 |1 |ckd |
+---+----+---+-----+---+---+-----+---+---+--------------+
现在问题清楚地表明,您不想为所有列重复该过程,因此可以执行以下操作
Now the question clearly says that you don't want to repeat the procedure for all the columns for that you can do the following
val columnsTomap = df.select("rbc", "cad", "rbc", "pe", "ane").columns
var tempdf = df
columnsTomap.map(column => {
tempdf = tempdf.withColumn(column, applyFunction(col(column)))
})
tempdf.show(false)
这篇关于将UDF应用于Spark Dataframe中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文