假设我有一个数据框:
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 做到这一点?
import pyspark.sql.functions as f
df.groupby("product_id").agg(f.collect_list("customer").alias("customers")).show()
Run Code Online (Sandbox Code Playgroud)
(编辑注释- 在代码中添加导入语句)
| 归档时间: |
|
| 查看次数: |
1586 次 |
| 最近记录: |