Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将函数应用于数组列pyspark中的所有值_Arrays_Apache Spark_Pyspark_User Defined Functions - Fatal编程技术网

Arrays 将函数应用于数组列pyspark中的所有值

Arrays 将函数应用于数组列pyspark中的所有值,arrays,apache-spark,pyspark,user-defined-functions,Arrays,Apache Spark,Pyspark,User Defined Functions,我想使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..paralleli

我想使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]|
    +----+---------------+

谢谢

只是你没有在列表中循环使用-1乘以它们

import pyspark.sql.functions as F
import pyspark.sql.types as T

negative = F.udf(lambda x: [i * -1 for i in x], T.ArrayType(T.FloatType()))
cast_contracts = df \
    .withColumn('forecast_values', negative('forecast_values'))
您不能逃避
udf
,但最好的方法是这样做。如果列表较大,效果会更好:

import numpy as np

negative = F.udf(lambda x: np.negative(x).tolist(), T.ArrayType(T.FloatType()))
cast_contracts = abdf \
    .withColumn('forecast_values', negative('forecast_values'))
cast_contracts.show()
+------------------+----+
|   forecast_values|name|
+------------------+----+
|[-1.0, -2.0, -3.0]| Joe|
|            [-3.0]|Mary|
|      [-4.0, -7.1]|Mary|
+------------------+----+

只是你没有循环列表中的值来乘以-1

import pyspark.sql.functions as F
import pyspark.sql.types as T

negative = F.udf(lambda x: [i * -1 for i in x], T.ArrayType(T.FloatType()))
cast_contracts = df \
    .withColumn('forecast_values', negative('forecast_values'))
您不能逃避
udf
,但最好的方法是这样做。如果列表较大,效果会更好:

import numpy as np

negative = F.udf(lambda x: np.negative(x).tolist(), T.ArrayType(T.FloatType()))
cast_contracts = abdf \
    .withColumn('forecast_values', negative('forecast_values'))
cast_contracts.show()
+------------------+----+
|   forecast_values|name|
+------------------+----+
|[-1.0, -2.0, -3.0]| Joe|
|            [-3.0]|Mary|
|      [-4.0, -7.1]|Mary|
+------------------+----+

我知道这是一个有一年历史的帖子,所以我要给出的解决方案以前可能不是一个选项(Spark 3是新的)。如果在PySpice API中使用Skice 3和以上,则应该考虑使用<代码> Skp.SQL.Fig.Trime内<代码> PySpab.SQL.Stury.ExpP<代码>。 请不要将
spark.sql.function.transform
与PySpark的
transform()
链接混淆。无论如何,以下是解决方案:

df.withColumn("negative", F.expr("transform(forecast_values, x -> x * -1)"))

唯一需要确保的是将值转换为int或float。强调的方法比分解数组或使用UDF更有效。

我知道这是一篇已有一年历史的文章,因此我要给出的解决方案以前可能不是一个选项(Spark 3是新的)。如果您在PySpark API中使用spark 3.0及以上版本,则应考虑在
PySpark.sql.functions.expr
内部使用
spark.sql.function.transform
。 请不要将
spark.sql.function.transform
与PySpark的
transform()
链接混淆。无论如何,以下是解决方案:

df.withColumn("negative", F.expr("transform(forecast_values, x -> x * -1)"))

唯一需要确保的是将值转换为int或float。突出显示的方法比分解数组或使用udf要有效得多。

negative=func.udf(lambda x:[i*-1代表i in x],T.ArrayType(T.FloatType())
??
negative=func.udf(lambda x:[i*-1代表i in x],T.ArrayType(T.FloatType())
??
谢谢。这将返回一个空数组。也许我的数组是一个字符串数组,我需要先把它转换成float。而且我的运行时间似乎增加了12分钟。你认为这可能仅仅是因为udf吗?@LN\P是的,udf会破坏你的性能,但是没有内置的功能来操作
数组
类型列。您正在处理多少行?
negative=F.udf(lambda x:[float(i)*-1表示x中的i],T.ArrayType(T.FloatType())
如果是字符串,谢谢。这将返回一个空数组。也许我的数组是一个字符串数组,我需要先把它转换成float。而且我的运行时间似乎增加了12分钟。你认为这可能仅仅是因为udf吗?@LN\P是的,udf会破坏你的性能,但是没有内置的功能来操作
数组
类型列。您正在处理多少行?
negative=F.udf(lambda x:[float(i)*-1表示x中的i],T.ArrayType(T.FloatType())
如果是字符串