how to rename the Columns Produced by count() function in Scala?

Hello Members

I have the below df:

+------+-------+--------+
|student|  vars|observed|
+------+-------+--------+
|  1|   ABC   |      19|
|  1|    ABC   |       1|
|  2|    CDB   |       1|
|  1|    ABC   |       8|
|   3|   XYZ   |       3|
|  1|    ABC   |     389|
|   2|   CDB   |     946|
|  1|    ABC   |     342|
|+------+-------+--------+

I wanted to add a new frequency column groupBy two columns “student”, “vars” in SCALA.

val frequency = df.groupBy($“student”, $“vars”).count()

This code generates a “count” column with the frequencies BUT losing observed column from the df.

I would like to create a new df as follows without losing “observed” column

±-----±------±-------±-----------+
|student| vars|observed|total_count |
±-----±------±-------±-----------+
| 1| ABC | 9|22
| 1| ABC | 1|22
| 2| CDB | 1|7
| 1| ABC | 2|22
| 3| XYZ | 3|3
| 1| ABC | 8|22
| 2| CDB | 6|7
| 1| ABC | 2|22
|±-----±------±------±-------------+

Thanks
Venkata