【问题标题】:Count ocurrences in pyspark dataframe计算 pyspark 数据框中的出现次数
【发布时间】:2020-09-08 23:33:05
【问题描述】:

我需要统计一个pyspark数据框中重复值的出现次数,如图所示。 简而言之,当值相同时,它会累加,直到值不同。当值不同时,计数被重置。我需要它在一个列中。

我有什么:

+------+
| val  |
+------+
| 0    | 
| 0    | 
| 0    |
| 1    |
| 1    |
| 2    |
| 2    |
| 2    |
| 3    |
| 3    |
| 3    |
| 3    |
+------+

我需要什么:

+------+-----+
| val  |ocurr| 
+------+-----+
| 0    | 0   | 
| 0    | 1   | 
| 0    | 2   |
| 1    | 0   |
| 1    | 1   |
| 2    | 0   |
| 2    | 1   |
| 2    | 2   |
| 3    | 0   |
| 3    | 1   |
| 3    | 2   |
| 3    | 3   |
+------+-----+

【问题讨论】:

    标签: python amazon-web-services pyspark apache-spark-sql aws-glue


    【解决方案1】:

    使用whenlag 函数对相同的并发值进行分组,并使用row_number 获取计数。您应该有一个适当的排序列,我的临时排序列id 不好,因为它不能保证保留顺序。

    df = spark.createDataFrame([0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 0, 0, 0], 'int').toDF('val')
    
    from pyspark.sql.functions import *
    from pyspark.sql import Window
    
    w1 = Window.orderBy('id')
    w2 = Window.partitionBy('group').orderBy('id')
    
    df.withColumn('id', monotonically_increasing_id()) \
      .withColumn('group', sum(when(col('val') == lag('val', 1, 1).over(w1), 0).otherwise(1)).over(w1)) \
      .withColumn('order', row_number().over(w2) - 1) \
      .orderBy('id').show()
    
    +---+---+-----+-----+
    |val| id|group|order|
    +---+---+-----+-----+
    |  0|  0|    1|    0|
    |  0|  1|    1|    1|
    |  0|  2|    1|    2|
    |  1|  3|    2|    0|
    |  1|  4|    2|    1|
    |  2|  5|    3|    0|
    |  2|  6|    3|    1|
    |  2|  7|    3|    2|
    |  3|  8|    4|    0|
    |  3|  9|    4|    1|
    |  3| 10|    4|    2|
    |  3| 11|    4|    3|
    |  0| 12|    5|    0|
    |  0| 13|    5|    1|
    |  0| 14|    5|    2|
    +---+---+-----+-----+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多