How to select the first row of each group?

前端 未结 8 784
心在旅途
心在旅途 2020-11-21 05:49

I have a DataFrame generated as follow:

df.groupBy($\"Hour\", $\"Category\")
  .agg(sum($\"value\") as \"TotalValue\")
  .sort($\"Hour\".asc, $\"TotalValue\"         


        
8条回答
  •  时光说笑
    2020-11-21 06:55

    This is a exact same of zero323's answer but in SQL query way.

    Assuming that dataframe is created and registered as

    df.createOrReplaceTempView("table")
    //+----+--------+----------+
    //|Hour|Category|TotalValue|
    //+----+--------+----------+
    //|0   |cat26   |30.9      |
    //|0   |cat13   |22.1      |
    //|0   |cat95   |19.6      |
    //|0   |cat105  |1.3       |
    //|1   |cat67   |28.5      |
    //|1   |cat4    |26.8      |
    //|1   |cat13   |12.6      |
    //|1   |cat23   |5.3       |
    //|2   |cat56   |39.6      |
    //|2   |cat40   |29.7      |
    //|2   |cat187  |27.9      |
    //|2   |cat68   |9.8       |
    //|3   |cat8    |35.6      |
    //+----+--------+----------+
    

    Window function :

    sqlContext.sql("select Hour, Category, TotalValue from (select *, row_number() OVER (PARTITION BY Hour ORDER BY TotalValue DESC) as rn  FROM table) tmp where rn = 1").show(false)
    //+----+--------+----------+
    //|Hour|Category|TotalValue|
    //+----+--------+----------+
    //|1   |cat67   |28.5      |
    //|3   |cat8    |35.6      |
    //|2   |cat56   |39.6      |
    //|0   |cat26   |30.9      |
    //+----+--------+----------+
    

    Plain SQL aggregation followed by join:

    sqlContext.sql("select Hour, first(Category) as Category, first(TotalValue) as TotalValue from " +
      "(select Hour, Category, TotalValue from table tmp1 " +
      "join " +
      "(select Hour as max_hour, max(TotalValue) as max_value from table group by Hour) tmp2 " +
      "on " +
      "tmp1.Hour = tmp2.max_hour and tmp1.TotalValue = tmp2.max_value) tmp3 " +
      "group by tmp3.Hour")
      .show(false)
    //+----+--------+----------+
    //|Hour|Category|TotalValue|
    //+----+--------+----------+
    //|1   |cat67   |28.5      |
    //|3   |cat8    |35.6      |
    //|2   |cat56   |39.6      |
    //|0   |cat26   |30.9      |
    //+----+--------+----------+
    

    Using ordering over structs:

    sqlContext.sql("select Hour, vs.Category, vs.TotalValue from (select Hour, max(struct(TotalValue, Category)) as vs from table group by Hour)").show(false)
    //+----+--------+----------+
    //|Hour|Category|TotalValue|
    //+----+--------+----------+
    //|1   |cat67   |28.5      |
    //|3   |cat8    |35.6      |
    //|2   |cat56   |39.6      |
    //|0   |cat26   |30.9      |
    //+----+--------+----------+
    

    DataSets way and don't dos are same as in original answer

提交回复
热议问题