使用groupby或aggregate在RDD或DataFrame中合并每个事务中的项来执行FPgrowth

2024-03-28 11:08:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把这个结构的数据帧改成第二个。在

+---+-----+-----+
| id|order|items|
+---+-----+-----+
|  0|    a|    1|
|  1|    a|    2|
|  2|    a|    5|
|  3|    b|    1|
|  4|    b|    2|
|  5|    b|    3|
|  6|    b|    5|
|  7|    c|    1|
|  8|    c|    2|
+---+-----+-----+

将其更改为:

^{pr2}$

在PySpark里怎么做?在


Tags: 数据idorderitems结构pysparkpr2
2条回答

在您的案例中,使用collect_list函数的Groupby顺序和带有{}的唯一id应该可以工作

from pyspark.sql import functions as F
df.groupBy("order").agg(F.collect_list("items"))
   .withColumn("id", F.row_number().over(Window.orderBy("order")))

希望这有帮助!在

你能做到的

from pyspark.sql.functions import *
df.groupBy(df.order).agg(collect_list("items").alias("items"))

已编辑

如果您想在rdd中执行相同的操作,可以执行以下操作(scala)

^{pr2}$

假设rdd为

(0,a,1)
(1,a,2)
(2,a,5)
(3,b,1)
(4,b,2)
(5,b,3)
(6,b,5)
(7,c,1)
(8,c,2)

结果是

((a,List(1, 2, 5)),0)
((b,List(1, 2, 3, 5)),1)
((c,List(1, 2)),2)

相关问题 更多 >