I have a simple csv file with columns(id:Integer,Name:String,Gender:String)
. I want to add a column to this dataframe using withColumn. I am aware that first parameter is column name but not quite sure how to pass the second parameter which will group by the gender. Can someone help me with this?
I have tried using df.withColumn("Count",df.groupby("Gender").count())
but it is giving type mismatch error saying it is expecting a column type but getting a dataframe. I have tried using col()
also expecting it to type cast it.
val newDf=df.withColumn("count",col(df.groupBy("Gender").count()))
I expect a new dataframe with count column added.