我有向量类型的列,每个向量中有一个值。我只想得到那个值并保持列为doubleType。你知道吗
输入df示例:
|testcol|
[1.3]|
[1.2]|
[3.4]|
期望输出df:
|testcol|
|1.3|
|1.2|
|3.4|
到目前为止我掌握的代码:
remove_vector_func = udf(lambda x: list(x)[0], DoubleType())
ex= ex.withColumn("testcol", remove_vector_func("testcol"))
此代码运行,但当我尝试显示列时,它会不断抛出错误:
expected zero arguments for construction of ClassDict (for numpy.dtype)
我在printSchema()中看到列类型是正确的:
testcol: double (nullable = true)
您只需要确保lambda函数返回与UDF的返回类型匹配的对象。在这种情况下,需要将对象转换为float类型
代码:
输出:
相关问题 更多 >
编程相关推荐