Adding a count column to my sequence in Scala

前端 未结 1 938
伪装坚强ぢ
伪装坚强ぢ 2021-01-07 13:57

Given the code below, how would I go about adding a count column? (e.g. .count(\"*\").as(\"count\"))

Final output to look like something like this:

+         


        
相关标签:
1条回答
  • 2021-01-07 14:49

    You can simply append count("*").as("count") to aggExprs.tail in your agg, as shown below:

    df.
      groupBy("id").agg(aggExprs.head, aggExprs.tail :+ count("*").as("count"): _*).
      show
    // +---+------+------+-----------------------------+-----+
    // | id|sum(d)|max(b)|concat_ws(,, collect_list(s))|count|
    // +---+------+------+-----------------------------+-----+
    // |  1|   1.0|  true|                            a|    1|
    // |  3|   3.0| false|                            b|    1|
    // |  2|   4.0| false|                          b,c|    2|
    // +---+------+------+-----------------------------+-----+
    
    0 讨论(0)
提交回复
热议问题