gpt4 book ai didi

python - Pyspark 加盐 : replace null in column with random negative values

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:24 25 4
gpt4 key购买 nike

我正在对许多列执行联接,这些列有时可能包含数十亿行空值,因此我想对列加盐以防止联接后出现倾斜,如 Jason Evan 的帖子中提到的:https://stackoverflow.com/a/43394695

我在 Python 中找不到等效的示例,而且语法差异很大,我不知道如何翻译它。

我大约有这个:

import pyspark.sql.functions as psf
big_neg = -200
for column in key_fields: #key_fields is a list of join keys in the dataframe
df = df.withColumn(column,
psf.when(psf.col(column).isNull(),
psf.round(psf.rand().multiply(big_neg))
).otherwise(df[column]))

目前由于语法错误而失败:

TypeError: 'Column' object is not callable

但是我已经尝试了很多语法组合来摆脱 typeError 并被难住了。

最佳答案

休息一下后我实际上能够弄清楚了。

我认为这会对遇到此问题的其他人有所帮助,因此我将发布我的解决方案:

df = df.withColumn(column, psf.when(df[column].isNull(), psf.round(psf.rand()*(big_neg))).otherwise(df[column]))

关于python - Pyspark 加盐 : replace null in column with random negative values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55170607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com