Apache spark 相当于Spark提供的pandas中的combine_first?
当满足某些条件时,我正在尝试用另一个数据帧更新Apache spark 相当于Spark提供的pandas中的combine_first?,apache-spark,dataframe,Apache Spark,Dataframe,当满足某些条件时,我正在尝试用另一个数据帧更新数据帧 pandasDataFrame中的combine\u first功能运行良好。Spark中是否有有效更新数据帧的等效方法?没有严格的等效方法,但如果您有一个公共密钥,您可以加入并合并: from pyspark.sql.functions import coalesce, col, isnan, when keys = ["index"] df1 = pd.DataFrame([[1, np.nan]]) df2 = pd.DataFra
数据帧
pandas
DataFrame
中的combine\u first
功能运行良好。Spark中是否有有效更新数据帧的等效方法?没有严格的等效方法,但如果您有一个公共密钥,您可以加入并合并:
from pyspark.sql.functions import coalesce, col, isnan, when
keys = ["index"]
df1 = pd.DataFrame([[1, np.nan]])
df2 = pd.DataFrame([[3, 4]])
sdf1 = spark.createDataFrame(df1.reset_index()).alias("df1")
sdf2 = spark.createDataFrame(df2.reset_index()).alias("df2")
def first_of(c1, c2):
return coalesce(when(~isnan(c1), c1), when(~isnan(c2), c2))
sdf1.join(sdf2, keys, "fullouter").select(keys + [
first_of(sdf1[c], sdf2[c]).alias(c) for c in sdf1.columns if c not in keys
]).show()
# +-----+---+---+
# |index| 0| 1|
# +-----+---+---+
# | 0| 1|4.0|
# +-----+---+---+
谢谢!这很有效。除了在任何一个数据帧中都有额外列的情况外,还需要在最终更新的数据帧中。是否可以相应地修改联接以实现此目的?还想提及列名是动态的。