python - pyspark 如果列具有相同的值,如何组合列值

标签 python python-3.x python-2.7 apache-spark pyspark

我有一个想法,使用相同的键组合不同行中的数据帧值(列表)。 该组合在不同行中必须具有相同或多个值,因此我不能仅使用 df.groupBy('id') 来获取结果。

示例如下:

+---------+--------------------+
|id       |num_list            |
+---------+--------------------+
|apple    |[11, 12]            |
|apple    |[11, 13 ,14]        |
|apple    |[10, 22, 25]        |
|banana   |[15, 26]            |
|banana   |[15, 29]            |
|banana   |[15, 27]            |
+---------+--------------------+

我们可以发现id=apple有两条记录和两个列表,例如:[11, 12],[11, 13, 14],所以它们会被合并到新记录 id=apple,num_list=[11, 12, 13, 14]

但是 id=apple,num_list=[10, 22, 25] 不会合并。

这就是我想要的答案:

+---------+--------------------+
|id       |num_list            |
+---------+--------------------+
|apple    |[11, 12, 13, 14]    |
|apple    |[10, 22, 25]        |
|banana   |[15, 26, 27, 29]    |
+---------+--------------------+

编辑:

有一些规则我必须解释一下。

正如@Usernamenotfound 评论的那样,

假设 apple 有三个值 [11, 12, 14][9, 13 ,14][12,13,27],答案将是 [9, 11, 12, 13 ,14, 27] 而不是 [9, 11, 12, 13 ,14] >[12,13,27]

有一些新的例子:

+--------------------+------------+
|                  id|num_list    |
+--------------------+------------+
|apple               |         [0]|
|apple               |         [0]|
|apple               |         [1]|
|apple               |         [1]|
|apple               |         [2]|
|apple               |         [3]|
|apple               |         [4]|
|apple               |         [5]|
|apple               |         [6]|
|apple               |         [6]|
|apple               |         [7]|
|apple               |      [7, 8]|
|apple               |         [9]|
|apple               |         [9]|
|apple               |         [9]|
|apple               |     [9, 10]|
|apple               | [9, 17, 18]|
|apple               |        [10]|
|apple               |        [10]|
|apple               |        [10]|
+--------------------+------------+

如果我尝试使用@mayank 的代码将会得到错误的答案。

+--------------------------------+---------------------------------------------------------------------------------------+
|                              id|num_list                                                                               |
+--------------------------------+---------------------------------------------------------------------------------------+
|apple                           |[0]                                                                                    |
|apple                           |[0]                                                                                    |
|apple                           |[1]                                                                                    |
|apple                           |[2]                                                                                    |
|apple                           |[3]                                                                                    |
|apple                           |[4]                                                                                    |
|apple                           |[5]                                                                                    |
|apple                           |[6]                                                                                    |
|apple                           |[8, 7]                                                                                 |
|apple                           |[9, 10, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 36, 37, 38]|
|apple                           |[12]                                                                                   |
|apple                           |[14]                                                                                   |
|apple                           |[24]                                                                                   |
|apple                           |[31]                                                                                   |
|apple                           |[32]                                                                                   |
|apple                           |[33, 34]                                                                               |
|apple                           |[35]                                                                                   |
|apple                           |[39]                                                                                   |
+--------------------------------+---------------------------------------------------------------------------------------+

任何帮助将不胜感激。

最佳答案

也许不是最有效的解决方案,但它解决了您的问题。

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

def get_combinations(lis):
   final = []

   for li in lis:
       if [y for y in final if [z for z in li if z in y]]:
           found = [y for y in final if [z for z in li if z in y]][0]
           to_add = list(set(found + li))
           final[final.index(found)] = to_add
       else:
           final.append(li)
   return final

apply_udf = F.udf(lambda x:get_combinations(x),ArrayType(ArrayType(IntegerType())))

df = df.groupby('id').agg(F.collect_list('num_list').alias('num_list'))\
        .select(['id', F.explode(apply_udf('num_list')).alias('num_list')])

关于python - pyspark 如果列具有相同的值,如何组合列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673889/

相关文章:

python - 如何迭代创建通用树的类属性?

python - 我固定的 pyOpenSSL 版本 19.0.0 突然坏了

django - 在模型中为Django字段指定类型(对于Pylint)

python - 如何装饰子类中的继承方法?

python - 找出对象的可能属性

python - PyTorch 错误 : CUDA error: CUBLAS_STATUS_INTERNAL_ERROR when calling `cublasCreate(handle)`

python - 加载预训练的 keras 模型以在 Google Cloud 上继续训练

python-3.x - Dark Sky API 在 Python 3 中每天迭代一年

Python - 记录多个文件

python - 如何在 Mac 上指定路径