【问题标题】:Call items inside list of PySpark data frame调用 PySpark 数据框列表中的项目
【发布时间】:2018-02-26 17:08:30
【问题描述】:

我有以下数据框

+------------------+----------+------------------+
|        antecedent|consequent|        confidence|
+------------------+----------+------------------+
|         [7, 2, 0]|       [8]|0.6237623762376238|
|         [7, 2, 0]|       [1]|               1.0|
|         [7, 2, 0]|       [5]|0.9975247524752475|
|         [7, 2, 0]|       [3]|0.9975247524752475|
|         [7, 2, 0]|       [4]|0.9975247524752475|
|         [7, 2, 0]|       [6]| 0.995049504950495|
|      [6, 5, 3, 4]|       [8]| 0.623721881390593|
|      [6, 5, 3, 4]|       [1]|               1.0|
|      [6, 5, 3, 4]|       [2]|               1.0|
|      [6, 5, 3, 4]|       [0]|               1.0|
|      [6, 5, 3, 4]|       [7]| 0.820040899795501|
|[9, 8, 6, 5, 1, 2]|       [0]|               1.0|
|[9, 8, 6, 5, 1, 2]|       [3]|               1.0|
|[9, 8, 6, 5, 1, 2]|       [4]|               1.0|
|         [7, 3, 1]|       [8]|0.6228287841191067|
|         [7, 3, 1]|       [5]|               1.0|
|         [7, 3, 1]|       [2]|               1.0|
|         [7, 3, 1]|       [0]|               1.0|
|         [7, 3, 1]|       [4]|               1.0|
|         [7, 3, 1]|       [6]|0.9950372208436724|
+------------------+----------+------------------+

我想对其进行一些查询,例如过滤前行不包含 [7,3] 的行,我尝试了此查询,但似乎错误,因为 7,3 是整数

from pyspark.sql.functions import *
q = r.filter(~col('antecedent').isin([7,3])).show()

错误:

"condition should be string or Column"

【问题讨论】:

  • 您是要单独检查 7 和 3 还是作为数组 [7, 3] 进行检查?
  • 我先尝试了 [3] ,然后 [3,7] 都没有成功

标签: pandas python-2.7 apache-spark apache-spark-sql pyspark-sql


【解决方案1】:

您可以编写一个 udf 函数来检查条件(检查 7 或 3 是否存在于先行词 column 中的条件)为

from pyspark.sql import functions as F
from pyspark.sql import types as T
def checkIsIn(array):
    return True in [x in array for x in [7, 3]]

udfCheckIsIn = F.udf(checkIsIn, T.BooleanType())

然后在过滤器中使用它作为

q = r.filter(udfCheckIsIn(r.antecedent)).show()

你的输出应该是

+------------+----------+------------------+
|  antecedent|consequent|        confidence|
+------------+----------+------------------+
|   [7, 2, 0]|       [8]|0.6237623762376238|
|   [7, 2, 0]|       [1]|               1.0|
|   [7, 2, 0]|       [5]|0.9975247524752475|
|   [7, 2, 0]|       [3]|0.9975247524752475|
|   [7, 2, 0]|       [4]|0.9975247524752475|
|   [7, 2, 0]|       [6]| 0.995049504950495|
|[6, 5, 3, 4]|       [8]| 0.623721881390593|
|[6, 5, 3, 4]|       [1]|               1.0|
|[6, 5, 3, 4]|       [2]|               1.0|
|[6, 5, 3, 4]|       [0]|               1.0|
|[6, 5, 3, 4]|       [7]| 0.820040899795501|
|   [7, 3, 1]|       [8]|0.6228287841191067|
|   [7, 3, 1]|       [5]|               1.0|
|   [7, 3, 1]|       [2]|               1.0|
|   [7, 3, 1]|       [0]|               1.0|
|   [7, 3, 1]|       [4]|               1.0|
|   [7, 3, 1]|       [6]|0.9950372208436724|
+------------+----------+------------------+

用于检查条件(检查 7 和 3 是否存在于先行词 column 中的条件)为

from pyspark.sql import functions as F
from pyspark.sql import types as T
def checkIsIn(array):
    return False not in [x in array for x in [7, 3]]

udfCheckIsIn = F.udf(checkIsIn, T.BooleanType())

然后在过滤器中使用它作为

q = r.filter(udfCheckIsIn(r.antecedent)).show()

你的输出应该是

+----------+----------+------------------+
|antecedent|consequent|        confidence|
+----------+----------+------------------+
| [7, 3, 1]|       [8]|0.6228287841191067|
| [7, 3, 1]|       [5]|               1.0|
| [7, 3, 1]|       [2]|               1.0|
| [7, 3, 1]|       [0]|               1.0|
| [7, 3, 1]|       [4]|               1.0|
| [7, 3, 1]|       [6]|0.9950372208436724|
+----------+----------+------------------+

【讨论】:

  • 谢谢,它看起来像“或”或并集,因为我得到所有包含 3 的项目,项目包含 7,项目包含 3&7 一起,第一个答案看起来像交集或 & 操作,所有答案很有用
  • @amal,这就是为什么我在问题评论中询问你的 && 或或。
  • 是的,我不明白这个问题,但这个答案对我也有帮助,因为我正在写论文
  • @amal,我已经添加了两种方式并且比以前更有效:) 请看一下
  • @amal,我知道这两个答案都对您有所帮助,因此您可以继续接受其中的任何一个。并且请不要忘记在可能的时候投票。 :)
猜你喜欢
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 2021-06-11
  • 2020-07-08
  • 1970-01-01
  • 2018-04-17
  • 2021-11-14
相关资源
最近更新 更多