【发布时间】:2020-02-18 14:38:20
【问题描述】:
我想让我的 pyspark 数据框中的数组列中的所有值都为负而不爆炸(!)。我试过这个 udf 但它没有用:
negative = func.udf(lambda x: x * -1, T.ArrayType(T.FloatType()))
cast_contracts = cast_contracts \
.withColumn('forecast_values', negative('forecast_values'))
有人可以帮忙吗?
示例数据框:
df = sc..parallelize(
[Row(name='Joe', forecast_values=[1.0,2.0,3.0]),
Row(name='Mary', forecast_values=[4.0,7.1])]).toDF()
>>> df.show()
+----+---------------+
|name|forecast_values|
+----+---------------+
| Joe|[1.0, 2.0, 3.0]|
|Mary| [4.0, 7.1]|
+----+---------------+
谢谢
【问题讨论】:
-
negative = func.udf(lambda x: [i * -1 for i in x], T.ArrayType(T.FloatType()))??
标签: arrays apache-spark pyspark user-defined-functions