我有两份数据如下。如果一个人买东西,我们也可以推荐类似的产品。
df1列出了每个人购买的物品清单。df2推荐了附加产品.例如,“高脚”买包子,然后我要推荐“黄油”和“果酱”。
如果没有added_product项(来自df2),那么它就不需要出现在输出中。因为(例如) "Gopu“购买了一个项目”饼干“,但没有添加从df2推荐的项目。因此,它将不会出现在输出表中。谢谢
简单的df1.df2靠左连接并不适合我。
df1:
name product
Gopu biscuit
Gopu bun
Gopu ink
Aish ball
Aish doll
Aish bun
Aish ink
Colin bun
Colin handsanitize
Colin paper
df2:
product added-product
bun butter
bun jam
ink cloth
ink bib
paper pen
doll barbie预期产出:
Name added-product
Gopu butter
Gopu jam
Gopu cloth
Gopu bib
Aish barbie
Aish butter
Aish jam
Aish cloth
Aish bib
Colin butter
Colin jam
Colin pen谢谢。
发布于 2020-03-13 07:09:43
dfnew=df1.join(df2,(df1.product==df2.product),"cross").select('name','added-product').orderBy('name')
dfnew.show()
+-----+-------------+
| name|added-product|
+-----+-------------+
| Aish| butter|
| Aish| jam|
| Aish| cloth|
| Aish| bib|
| Aish| barbie|
|Colin| jam|
|Colin| pen|
|Colin| butter|
| Gopu| butter|
| Gopu| cloth|
| Gopu| jam|
| Gopu| bib|
+-----+-------------+https://stackoverflow.com/questions/60665422
复制相似问题