如何根据Pyspark中数据框中的条件设置新的列表值?

时间:2017-12-18 13:15:48

标签: apache-spark pyspark apache-spark-sql spark-dataframe pyspark-sql

我有一个如下所示的DataFrame。

+---+------------------------------------------+
|id |features                                  |
+---+------------------------------------------+
|1  |[6.629056, 0.26771536, 0.79063195,0.8923] |
|2  |[1.4850719, 0.66458416, -2.1034079]       |
|3  |[3.0975454, 1.571849, 1.9053307]          |
|4  |[2.526619, -0.33559006, -1.4565022]       |
|5  |[-0.9286196, -0.57326394, 4.481531]       |
|6  |[3.594114, 1.3512149, 1.6967168]          |
+---+------------------------------------------+

我想根据下面的条件设置我的一些功能值。即其中id=1id=2id=6

我想在id=1设置新功能值,我当前的功能值为[6.629056, 0.26771536, 0.79063195,0.8923],但我想设置[0,0,0,0]

我想在id=2设置新功能值,我当前的功能值为[1.4850719, 0.66458416, -2.1034079],但我想设置[0,0,0]

我的最终出局将是:

+------+-----------------------------------+
|id  | features                            |
+-----+---------------------------------- -+
|1  | [0, 0, 0, 0]                          |
|2  | [0,0,0]                              |
|3  | [3.0975454, 1.571849, 1.9053307]     |
|4  | [2.526619, -0.33559006, -1.4565022]  |
|5  | [-0.9286196, -0.57326394, 4.481531]  |
|6  | [0,0,0]                              |
+-----+------------------------------------+

2 个答案:

答案 0 :(得分:3)

如果你有一组有限的id,你知道相应的feature的长度,那么Shaido的回答很好。

如果不是这样,那么使用UDF应该更清晰,并且您要转换的id可以加载到另一个Seq中:

在Scala中

val arr = Seq(1,2,6)

val fillArray = udf { (id: Int, array: WrappedArray[Double] ) =>
                        if (arr.contains(id) ) Seq.fill[Double](array.length)(0.0) 
                        else array 
                     }

df.withColumn("new_features" , fillArray($"id", $"features") ).show(false)

在Python中

from pyspark.sql import functions as f
from pyspark.sql.types import *

arr = [1,2,6]

def fillArray(id, features):
    if(id in arr): return [0.0] * len(features)
    else : return features

fill_array_udf = f.udf(fillArray, ArrayType( DoubleType() ) )

 df.withColumn("new_features" , fill_array_udf( f.col("id"), f.col("features") ) ).show()

<强>输出

+---+------------------------------------------+-----------------------------------+
|id |features                                  |new_features                       |
+---+------------------------------------------+-----------------------------------+
|1  |[6.629056, 0.26771536, 0.79063195, 0.8923]|[0.0, 0.0, 0.0, 0.0]               |
|2  |[1.4850719, 0.66458416, -2.1034079]       |[0.0, 0.0, 0.0]                    |
|3  |[3.0975454, 1.571849, 1.9053307]          |[3.0975454, 1.571849, 1.9053307]   |
|4  |[2.526619, -0.33559006, -1.4565022]       |[2.526619, -0.33559006, -1.4565022]|
|5  |[-0.9286196, -0.57326394, 4.481531]       |[-0.9286196, -0.57326394, 4.481531]|
|6  |[3.594114, 1.3512149, 1.6967168]          |[0.0, 0.0, 0.0]                    |
+---+------------------------------------------+-----------------------------------+

答案 1 :(得分:1)

如果您要更改一小组ID,请使用whenotherwise

df.withColumn("features", 
  when(df.id === 1, array(lit(0), lit(0), lit(0), lit(0)))
  .when(df.id === 2 | df.id === 6, array(lit(0), lit(0), lit(0)))
  .otherwise(df.features)))

它应该比UDF快,但是如果有很多ID需要更改它很快会变成很多代码。在这种情况下,请在philantrovert的答案中使用UDF