I have a PySpark dataframe and would like to groupby several columns and then calculate the sum of some columns and count distinct values of another column. As countDistinct is not a build in aggregation function, I can't use simple expressions like the ones I tried here:
sum_cols = ['a', 'b']
count_cols = ['id']
exprs1 = {x: "sum" for x in sum_cols}
exprs2 = {x: "countDistinct" for x in count_cols}
exprs = {**exprs1, **exprs2}
df_aggregated = df.groupby('month','product').agg(exprs)
I also tried the approach from this answer as exprs2 = [countDistinct(x) for x in count_cols] but I received an error message when I AssertionError: all exprs should be Column, when I tried it only for the aggregation column.
How could I combine sum and count distinct in one aggregation? I know, that I could do it once with the sum columns and once with the countDistinct columns and than join both dataframes but there should be a solution to do that in one step...