通过 ID 连接后,我的数据框如下所示:
ID | Features | Vector
1 | (50,[...] | Array[1.1,2.3,...]
2 | (50,[...] | Null
Run Code Online (Sandbox Code Playgroud)
我最终得到了“向量”列中某些 ID 的空值。我想用 300 维的零数组替换这些 Null 值(与非空向量条目的格式相同)。df.fillna 在这里不起作用,因为它是我想插入的数组。知道如何在 PySpark 中实现这一点吗?
- -编辑 - -
与这篇文章类似,我目前的方法是:
df_joined = id_feat_vec.join(new_vec_df, "id", how="left_outer")
fill_with_vector = udf(lambda x: x if x is not None else np.zeros(300),
ArrayType(DoubleType()))
df_new = df_joined.withColumn("vector", fill_with_vector("vector"))
Run Code Online (Sandbox Code Playgroud)
不幸的是,收效甚微:
org.apache.spark.SparkException: Job aborted due to stage failure: Task 0in stage 848.0 failed 4 times, most recent failure: Lost task 0.3 in stage …Run Code Online (Sandbox Code Playgroud)