如何使用 monotonically_increasing_id 来连接两个没有公共列的 pyspark 数据框?

Ajg*_*Ajg 1 join dataframe apache-spark pyspark

我有两个具有相同行数的 pyspark 数据框,但它们没有任何公共列。所以我使用 monotonically_increasing_id() 向它们添加新列

from pyspark.sql.functions import monotonically_increasing_id as mi
id=mi()
df1 = df1.withColumn("match_id", id)
cont_data = cont_data.withColumn("match_id", id)
cont_data = cont_data.join(df1,df1.match_id==cont_data.match_id, 'inner').drop(df1.match_id)
Run Code Online (Sandbox Code Playgroud)

但连接后生成的数据帧的行数较少。我在这里缺少什么。谢谢

小智 5

你只是不这样做。这不是 的适用用例monotonically_increasing_id,根据定义,它是不确定的。反而:

  • 转换为RDD
  • zipWithIndex
  • 转换回DataFrame.
  • join