0

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:

+---+------+------+-----------------------------+------
| id|sum(d)|max(b)|concat_ws(,, collect_list(s))|count|
+---+------+------+-----------------------------+------
|  1|   1.0|  true|                          a. | 1 |
|  2|   4.0|  true|                          b,b| 2 |
|  3|   3.0|  true|                          c. | 1 |

Current code is below:

val df =Seq(
  (1, 1.0, true, "a"),
  (2, 2.0, false, "b")
  (3, 3.0, false, "b")
  (2, 2.0, false, "c")
).toDF("id","d","b","s")

val dataTypes: Map[String, DataType] = df.schema.map(sf => (sf.name,sf.dataType)).toMap

def genericAgg(c:String) = {
  dataTypes(c) match {
    case DoubleType => sum(col(c))
    case StringType => concat_ws(",",collect_list(col(c))) // "append"
    case BooleanType => max(col(c))
  }
}

val aggExprs: Seq[Column] = df.columns.filterNot(_=="id")
.map(c => genericAgg(c))

df
.groupBy("id")
.agg(aggExprs.head,aggExprs.tail:_*)
  .show()

1 Answer 1

1

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|
// +---+------+------+-----------------------------+-----+
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.