Here is a direct way to get min and max from a data frame with column names:
val df = Seq((1, 2), (3, 4), (5, 6)).toDF("A", "B") df.show() /* +---+---+ | A| B| +---+---+ | 1| 2| | 3| 4| | 5| 6| +---+---+ */ df.agg(min("A"), max("A")).show() /* +------+------+ |min(A)|max(A)| +------+------+ | 1| 5| +------+------+ */
If you want to get the min and max values โโas separate variables, you can convert the result of agg() above to Row and use Row.getInt(index) to get the values โโof the Row column.
val min_max = df.agg(min("A"), max("A")).head() // min_max: org.apache.spark.sql.Row = [1,5] val col_min = min_max.getInt(0) // col_min: Int = 1 val col_max = min_max.getInt(1) // col_max: Int = 5
source share