How to select the first row of each group?

前端 未结 8 786
心在旅途
心在旅途 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:34

    Here you can do like this -

       val data = df.groupBy("Hour").agg(first("Hour").as("_1"),first("Category").as("Category"),first("TotalValue").as("TotalValue")).drop("Hour")
    
    data.withColumnRenamed("_1","Hour").show
    

提交回复
热议问题