I have a question,
I have a dataset like this :
id / color
1 / red
2 / green
2 / green
2 / blue
3 / blue
4 / yellow
4 / pink
5 / red
and I would like to group by id and to keep the most frequent color to have something like this : (in case of draw take randomly it's ok, or better solution if you have)
id / most_color
1 / red
2 / green
3 / blue
4 / yellow
5 / red
I have tried things like :
display(dataset.select("id","color").
dropDuplicates().
withColumn("most_color",count("color").over(w)))
or like this :
dataset2= (dataset.select("id","color").
withColumn("most_color", dataset["color"]).
groupBy("id").
agg(count('color').
alias('count').
filter(column('count') == max(count))))
display(dataset2)
thank you everyone