I have the following dataframe, where I hope to compare the value in a column to a string, and use it as a condition in if-else statement.
dataframe:
id type name
1 fruit apple
2 toy football
3 fruit orange
what I am hoping to achieve is:
if(df("type") == "fruit"){
//do something to the df
}else if ( df("type") == "toy"){
//something else to the df
}
I tried to use val type= df.select("type").collectAsList().getString(0)
but that's not correct. Could someone please help? Many thanks. I don't think it's a duplicate to this question as I do not want to add a new column. Spark: Add column to dataframe conditionally and I do not wish to use withColumn