Home > Net >  How to get the mode of a column from a dataframe using spark scala?
How to get the mode of a column from a dataframe using spark scala?

Time:12-12

I'm trying to get the mode of a column in a dataframe using spark scala but my code is not working.

For example

val type_mode = dfairports.groupBy("type").count().orderBy("count").first()
print("Mode", type_mode.get(0))

CodePudding user response:

You're almost there! You're probably getting the least common value now, since the orderBy function by default orders by ascending values. So taking the first element will take the lowest value.

Try:

val type_mode = dfairports.groupBy("type").count().orderBy(desc("count")).first()
print("Mode", type_mode.get(0))

Hope this helps :)

  • Related