如何将 PySpark 中的列分组到列表中?

mom*_*ind 2 dataframe pyspark

假设我有一个数据框:

product_id  customer
1 1
1 2
1 4
2 1
2 2
Run Code Online (Sandbox Code Playgroud)

我想将上述数据框分组为:

product_id customers
1 [1,2,4]
2 [1,2]
Run Code Online (Sandbox Code Playgroud)

我如何使用 PySpark 做到这一点?

Pre*_*rem 6

import pyspark.sql.functions as f 
df.groupby("product_id").agg(f.collect_list("customer").alias("customers")).show()
Run Code Online (Sandbox Code Playgroud)

编辑注释- 在代码中添加导入语句)