Q:

Different aggregate operations on different columns pyspark

I am trying to apply different aggregation functions to different columns in a pyspark dataframe.  Following some suggestions on stackoverflow, I tried this:
the_columns = ["product1","product2"]
the_columns2 = ["customer1","customer2"]

exprs = [mean(col(d)) for d in the_columns1, count(col(c)) for c in the_columns2] 

followed by 
 df.groupby(*group).agg(*exprs)

where "group" is a column not present in either the_columns or the_columns2.  This does not work.  How to do different aggregation functions on different columns?

A:

You are very close already, instead of put the expressions in a list, add them so you have a flat list of expressions:
exprs = [mean(col(d)) for d in the_columns1] + [count(col(c)) for c in the_columns2] 

Here is a demo:
import pyspark.sql.functions as F

df.show()
+---+---+---+---+
|  a|  b|  c|  d|
+---+---+---+---+
|  1|  1|  2|  1|
|  1|  2|  2|  2|
|  2|  3|  3|  3|
|  2|  4|  3|  4|
+---+---+---+---+

cols = ['b']
cols2 = ['c', 'd']    

exprs = [F.mean(F.col(x)) for x in cols] + [F.count(F.col(x)) for x in cols2]

df.groupBy('a').agg(*exprs).show()
+---+------+--------+--------+
|  a|avg(b)|count(c)|count(d)|
+---+------+--------+--------+
|  1|   1.5|       2|       2|
|  2|   3.5|       2|       2|
+---+------+--------+--------+

