get min and max from a specific column scala spark dataframe

后端 未结 7 1192
梦谈多话
梦谈多话 2021-02-01 04:37

I would like to access to the min and max of a specific column from my dataframe but I don\'t have the header of the column, just its number, so I should I do using scala ?

7条回答
  •  情深已故
    2021-02-01 04:56

    You can use the column number to extract the column names first (by indexing df.columns), then aggregate use the column names:

    val df = Seq((2.0, 2.1), (1.2, 1.4)).toDF("A", "B")
    // df: org.apache.spark.sql.DataFrame = [A: double, B: double]
    
    df.agg(max(df(df.columns(1))), min(df(df.columns(1)))).show
    +------+------+
    
    |max(B)|min(B)|
    +------+------+
    |   2.1|   1.4|
    +------+------+
    

提交回复
热议问题