【问题标题】:PySpark - Struggling to arrange the data by a specific formatPySpark - 努力按特定格式排列数据
【发布时间】:2021-10-05 21:26:55
【问题描述】:

我正在努力从预聚合帧中输出总的去重计数,如下所示。

我目前有一个这样显示的数据框。这是我通过过滤掉不需要的列而得到的初始结构和要点。

ID Source
101 Grape
101 Flower
102 Bee
103 Peach
105 Flower

我们可以从上面的示例中看到,在 Grape 和 Flower 中都可以找到 101。我想安排格式,以便“源”列中的不同字符串值成为它们自己的源,因为从那里我可以执行 groupBy 来进行特定的 yes 和 no 的排列。

ID Grape Flower Bee Peach
101 Yes Yes No No
102 No No Yes No
103 No No No Yes

我同意通过上面的示例手动创建它是一个不错的选择,但我正在处理 +100m 行并且需要更合适的东西。

到目前为止,我提取的是一个不同 Source 值的列表,并将它们排列成一个列表:

dedupeTableColumnNames = dedupeTable.select('SOURCE').distinct().collect()
dedupeTableColumnNamesCleaned = re.findall(r"'([^']*)'", str(dedupeTableColumnNames))

【问题讨论】:

    标签: pyspark


    【解决方案1】:

    这只是一个支点:

    df.groupBy("id").pivot("source").count().show()
    +---+------+------+------+------+                                               
    | id|Bee   |Flower|Grape |Peach |
    +---+------+------+------+------+
    |103|  null|  null|  null|     1|
    |105|  null|     1|  null|  null|
    |101|  null|     1|     1|  null|
    |102|     1|  null|  null|  null|
    +---+------+------+------+------+
    

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 2019-03-04
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2021-11-23
      相关资源
      最近更新 更多