I want to filter out rows in a dataframe where a column is not part of a list.
I am aware that I can use udf to go about this and it works.
def filterNegatives(val: Seq[String]): Seq[String] = {
val.filter(v => !badList.contains(v))
}
val filterNegativesUdf = udf(filterNegatives _, ArrayType(StringType))
val cleanedDF = myDF.withColumn("pos" , filterNegativesUdf(col("allVals")))
Was wondering if there is a non udf way of achieving this.
I have tried the following and it works.
val cleanedDF = myDF.withColumn("pos", expr(s"filter(allVals, val -> val NOT IN ('badval1', 'badval2'))"))
but my list badList contains ~10 elements and I'd rather keep it the code clean by defining a list.
I have tried using the list inside filter in different variations, but all of them had some errors.
.withColumn("pos", expr(s"filter(allVals, val NOT IN ${badList}"))
//error:no viable alternative at input 'NOT IN List'
Using - scala version 2.11