Dataframe Pypark在循环中重复

Dataframe Pypark在循环中重复,dataframe,apache-spark,optimization,pyspark,Dataframe,Apache Spark,Optimization,Pyspark,我在pyspark中有两个数据帧,我检查数据帧A中的数据,如果列为null,则用数据帧B中的同一列替换null数据 两个数据帧都有唯一的ID列,根据我加入的数据帧和下面的代码工作正常 updated_data = TABLE_BY_updated_date_unique.select('name_id_forwarded','name_id','name_id_org','first','last','passport','PHONE','EMAIL') most_attributes_data

我在pyspark中有两个数据帧,我检查数据帧A中的数据,如果列为null,则用数据帧B中的同一列替换null数据

两个数据帧都有唯一的ID列,根据我加入的数据帧和下面的代码工作正常

updated_data = TABLE_BY_updated_date_unique.select('name_id_forwarded','name_id','name_id_org','first','last','passport','PHONE','EMAIL')
most_attributes_data = Most_attributes.select('name_id_forwarded','name_id','name_id_org','first','last','passport','PHONE','EMAIL')

final_df = updated_data.alias('a').join(most_attributes_data.alias('b'), on=['name_id_forwarded'], how='left')\
    .select(
        'a.name_id_forwarded','a.name_id','a.name_id_org',
        f.when(f.isnull(f.col('a.first')),f.col('b.first')).otherwise(f.col('a.first')).alias('first'),      
  f.when(f.isnull(f.col('a.last')),f.col('b.last')).otherwise(f.col('a.last')).alias('last'),
  f.when(f.isnull(f.col('a.passport')),f.col('b.passport')).otherwise(f.col('a.passport')).alias('passport'),
  f.when(f.isnull(f.col('a.PHONE')),f.col('b.PHONE')).otherwise(f.col('a.PHONE')).alias('PHONE'),
  f.when(f.isnull(f.col('a.EMAIL')),f.col('b.EMAIL')).otherwise(f.col('a.EMAIL')).alias('EMAIL')
  )
我有40多列,我不想为每一列重复下面的代码。f、 当.isnullf.col'a.EMAIL',f.col'b.EMAIL'。其他为f.col'a.EMAIL'。别名为'EMAIL'

你能帮我循环一下这个语法吗?这样我就可以不重复读所有的列了* 在这种情况下使用coalesce函数动态生成表达式,然后将其与一起使用。选择

例如:

更新:

我们使用函数替换第一个非空值

在这种情况下,如果值为null,则需要替换b数据帧值,否则,如果值不为null,则需要替换一个值

在coalesce中,我们需要提到coalesceb

如果b.first值为空,则将使用a.first值。 如果不是b,则使用第一个值。 使用列表理解 [coalescef'df1.{i}',f'df.{i}.别名f'{i}'表示df.columns中的i,如果i!='id']动态创建具有df1b、dfa数据帧(不包括id列)的coalesce表达式,因为我们正在连接此列

然后将id列添加到列表中[i为df.columns中的i,如果i=='id']

我们现在使用创建了表达式。选择我们将在join之后执行上述步骤中准备的表达式。选择*expr.

Define list "


你能稍微解释一下吗?我不理解它?@aamirmalik127,当然,请检查我的更新答案。我添加了有关后续步骤的更多详细信息。如果您有任何问题,请告诉我!这并不能解决我的问题,实际上我不能用coalesce编写我所需的代码并在loog中使用它…你能帮忙吗?我需要在循环中的40列使用下面的syntaxt。f、 当.isnullf.col'a.EMAIL',f.col'b.EMAIL'。其他为f.col'a.EMAIL'。别名为'EMAIL'
from pyspark.sql.types import *
from pyspark.sql.functions import *

df=spark.createDataFrame([(1,'a'),(2,None),(3,10000)],['id','name','salary'])
df.show()
#+---+----+------+
#| id|name|salary|
#+---+----+------+
#|  1|   a|    10|
#|  2|null|   100|
#|  3|   b| 10000|
#+---+----+------+
df1=spark.createDataFrame([(1,'a',20),(2,'b',None),(3,None,100)],['id','name','salary'])

df1.show()
#+---+----+------+
#| id|name|salary|
#+---+----+------+
#|  1|   a|    20|
#|  2|   b|  null|
#|  3|null|   100|
#+---+----+------+

df.alias("df").join(df1.alias("df1"),['id'],'left').select('id',*expr).show()

expr=[i for i in df.columns if i=='id'] + [coalesce(f'df1.{i}',f'df.{i}').alias(f'{i}') for i in df.columns if i !='id']

#['id', Column<b'coalesce(df1.name, df.name) AS `name`'>, Column<b'coalesce(df1.salary, df.salary) AS `salary`'>]

df.alias("df").\
join(df1.alias("df1"),['id'],'left').\
select(*expr).\
show()
#+---+----+------+
#| id|name|salary|
#+---+----+------+
#|  1|   a|    20|
#|  3|   b|   100|
#|  2|   b|   100|
#+---+----+------+
col_list_1 = ['a.name_id','a.SUM','a.full_name','a.updated']

col_list_2 = ['first_name', 'last_name', 'email', 'phone_number']


colExpr = col_list_1 + list(map(lambda x: "nvl(a.{},b.{}) as {}".format(x,x,x),col_list_2))
      

Unique_With_AllCols = TABLE_BY_updated_date_unique.alias('a').\
                   join(Most_attributes.alias('b'), on=['name_id_forwarded'], 
                   how='left').selectExpr(*colExpr)