繁体   English   中英

pyspark 加入 2 个查找表

[英]pyspark join with 2 lookup tables

我在两个查找表中有一个销售数据和产品详细信息

df_prod_lookup1

ID     product     description
1      cereal      Minipack
2      canola      bottle
4      rice        bag

df_prod_lookup2

ID     product     description
6      glass       bottle
8      plants      hibiscus
10     tree        banyan

sales_df

ID     product     
10     tree        
1      cereal      
4      rice        
8      plants 

预计 output:

ID     product     description
10     tree        banyan
1      cereal      Minipack
4      rice        bag
8      plants      hibiscus

如果 ID 在查找表 1 中不可用,我应该使用查找表 1 和后来的查找表 2

查找表 1 和 2 的列名不同,不能合并为一个。 是否有一种方法可以检查 ID 在查找表 1 中是否可用,如果没有则进行连接,然后为销售中的每条记录查找表 2? 谢谢。

我只能用一个查找表进行简单的连接。

df_final = sales_df.join(df_prod_lookup1 on=['ID'], how='left')

问候

先左连接查找表 1,然后左连接查找表 2。
coalesce function 允许您合并description字段。

df_prod_lookup1 = df_prod_lookup1.withColumnRenamed("product", "product1").withColumnRenamed("description", "description1")
df_prod_lookup2 = df_prod_lookup2.withColumnRenamed("product", "product2").withColumnRenamed("description", "description2")

from pyspark.sql.functions import coalesce

# Edit based on comments #
sales_df.join(df_prod_lookup1, on=['ID'], how='left')\
        .join(df_prod_lookup2, on=['ID'], how='left')\
        .withColumn('product', coalesce('product1', 'product2'))\
        .withColumn('description', coalesce('description1', 'description2'))\
        .drop('product1', 'product2', 'description1', 'description2').show()

+---+-------+-----------+
| ID|product|description|
+---+-------+-----------+
|  8| plants|   hibiscus|
|  1| cereal|   Minipack|
| 10|   tree|     banyan|
|  4|   rice|        bag|
+---+-------+-----------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM