我使用的是Pyspark 2.1.0版本。
我尝试使用以下代码对两个数据框进行左外连接:
我有两个数据框,它们的架构如下所示:
crimes
|-- CRIME_ID: string (nullable = true)
|-- YEAR_MTH: string (nullable = true)
|-- CRIME_TYPE: string (nullable = true)
|-- CURRENT_OUTCOME: string (nullable = true)
outcomes
|-- CRIME_ID: string (nullable = true)
|-- YEAR_MTH: string (nullable = true)
|-- FINAL_OUTCOME: string (nullable = true)
我需要能够根据左外连接将犯罪与结果进行关联,因为一个犯罪可能对应多个结果。我希望排除两个数据框中共同的列。
我尝试了以下两种方法,但是每种方法都会产生各种错误:
cr_outs = crimes.join(outcomes, crimes.CRIME_ID == outcomes.CRIME_ID, 'left_outer')\
.select(['crimes.'+c for c in crimes.columns] + ['outcomes.FINAL_OUTCOME'])
from pyspark.sql.functions as fn
cr_outs = crimes.alias('a').join(outcomes.alias('b'), fn.col('b.CRIME_ID') = fn.col('a.CRIME_ID') ,'left_outer')\
.select([fn.col('a.'+ c) for c in a.columns] + b.FINAL_OUTCOME)
有没有人能够建议一种替代方法? 谢谢。
YEAR_MTH
列?(连接...) - muon