How to get other columns when using Spark DataFrame groupby?

前端 未结 7 1708
甜味超标
甜味超标 2020-11-29 22:04

when I use DataFrame groupby like this:

df.groupBy(df(\"age\")).agg(Map(\"id\"->\"count\"))

I will only get a DataFrame with columns \"a

7条回答
  •  借酒劲吻你
    2020-11-29 22:10

    One way to get all columns after doing a groupBy is to use join function.

    feature_group = ['name', 'age']
    data_counts = df.groupBy(feature_group).count().alias("counts")
    data_joined = df.join(data_counts, feature_group)
    

    data_joined will now have all columns including the count values.

提交回复
热议问题