【发布时间】:2021-07-31 14:47:03
【问题描述】:
我不明白如何解决这个问题,我已经在这里解决了一些问题,但没有找到完美的答案。
我有一个数据框,其中包含以下重要列:building_id、area、height。
我尝试编写的 UDF 计算面积的平方根和高度之间的差。它返回一个值,该值应添加到数据框中。
def calculate_difference(area, height):
# calculate the square root of the area
import numpy as np
nr = np.sqrt(area)
# calculate the difference between the square root of the area and the height
dif = nr - height
return dif
然后我注册这个 UDF:
calculate_differenceUDF = udf(calculate_difference)
当我传递两个数字时,该函数会起作用,它会返回我期望的值。我想在我的数据框中添加一个新列,其中我们有一个基于函数的计算值。
display(df.withColumn("diff", calculate_differenceUDF(col("area"), col("height"))))
然后我收到此错误:
PickleException:构造 ClassDict 的预期参数为零 (对于 numpy.dtype)
我知道我没有返回正确的类型,但我不知道如何解决它! :)
【问题讨论】:
标签: pyspark user-defined-functions databricks