如何通过比较两个pyspark数据帧来获得共同价值
How to get the common value by comparing two pyspark dataframes
我正在将 pandas 数据框迁移到 pyspark。我在 pyspark 中有两个不同计数的数据框。下面的代码我可以在 pandas 中实现,但不能在 pyspark 中实现。如何比较 pyspark 中的 2 个数据帧值并将该值作为新列放入 df2 中。
def impute_value (row,df_custom):
for index,row_custom in df_custom.iterrows():
if row_custom["Identifier"] == row["IDENTIFIER"]:
row["NEW_VALUE"] = row_custom['CUSTOM_VALUE']
return row["NEW_VALUE"]
df2['VALUE'] = df2.apply(lambda row: impute_value(row, df_custom),axis =1)
如何将此特定函数转换为 pyspark 数据帧?在 pyspark 中,我无法将按行值传递给函数 (impute_value).
我尝试了以下方法。
df3= df2.join(df_custom, df2["IDENTIFIER"]=df_custom["Identifier"],"left")
df3.WithColumnRenamed("CUSTOM_VALUE","NEW_VALUE")
这不是给我结果。
左连接本身应该做必要的事情
import pyspark.sql.functions as f
df3= df2.join(df_custom.withColumnRenamed('Identifier','Id'), df2["IDENTIFIER"]=df_custom["Id"],"left")
df3=df3.withColumn('NEW_VALUE',f.col('CUSTOM_VALUE')).drop('CUSTOM_VALUE','Id')
我正在将 pandas 数据框迁移到 pyspark。我在 pyspark 中有两个不同计数的数据框。下面的代码我可以在 pandas 中实现,但不能在 pyspark 中实现。如何比较 pyspark 中的 2 个数据帧值并将该值作为新列放入 df2 中。
def impute_value (row,df_custom):
for index,row_custom in df_custom.iterrows():
if row_custom["Identifier"] == row["IDENTIFIER"]:
row["NEW_VALUE"] = row_custom['CUSTOM_VALUE']
return row["NEW_VALUE"]
df2['VALUE'] = df2.apply(lambda row: impute_value(row, df_custom),axis =1)
如何将此特定函数转换为 pyspark 数据帧?在 pyspark 中,我无法将按行值传递给函数 (impute_value).
我尝试了以下方法。
df3= df2.join(df_custom, df2["IDENTIFIER"]=df_custom["Identifier"],"left")
df3.WithColumnRenamed("CUSTOM_VALUE","NEW_VALUE")
这不是给我结果。
左连接本身应该做必要的事情
import pyspark.sql.functions as f
df3= df2.join(df_custom.withColumnRenamed('Identifier','Id'), df2["IDENTIFIER"]=df_custom["Id"],"left")
df3=df3.withColumn('NEW_VALUE',f.col('CUSTOM_VALUE')).drop('CUSTOM_VALUE','Id')