How to calculate sum and count in a single groupBy?

前端 未结 3 581
醉梦人生
醉梦人生 2020-12-28 16:03

Based on the following DataFrame:

val client = Seq((1,\"A\",10),(2,\"A\",5),(3,\"B\",56)).toDF(\"ID\",\"Categ\",\"Amnt\")
+---+-----+----+
| ID|         


        
3条回答
  •  太阳男子
    2020-12-28 16:38

    You can do aggregation like below on given table:

    client.groupBy("Categ").agg(sum("Amnt"),count("ID")).show()
    
    +-----+---------+---------+
    |Categ|sum(Amnt)|count(ID)|
    +-----+---------+---------+
    |    A|       15|        2|
    |    B|       56|        1|
    +-----+---------+---------+
    

提交回复
热议问题