gpt4 book ai didi

count - pyspark:在一个窗口中计数不相同

转载 作者:行者123 更新时间:2023-12-03 15:07:58 52 4
gpt4 key购买 nike

我只是尝试在一个窗口上做一个countDistinct并得到了这个错误:

AnalysisException: u'Distinct window functions are not supported: count(distinct color#1926)

有没有办法对pyspark中的窗口进行不同的计数?

这是一些示例代码:
from pyspark.sql.window import Window    
from pyspark.sql import functions as F

#function to calculate number of seconds from number of days
days = lambda i: i * 86400

df = spark.createDataFrame([(17, "2017-03-10T15:27:18+00:00", "orange"),
(13, "2017-03-15T12:27:18+00:00", "red"),
(25, "2017-03-18T11:27:18+00:00", "red")],
["dollars", "timestampGMT", "color"])

df = df.withColumn('timestampGMT', df.timestampGMT.cast('timestamp'))

#create window by casting timestamp to long (number of seconds)
w = (Window.orderBy(F.col("timestampGMT").cast('long')).rangeBetween(-days(7), 0))

df = df.withColumn('distinct_color_count_over_the_last_week', F.countDistinct("color").over(w))

df.show()

这是我想看到的输出:
+-------+--------------------+------+---------------------------------------+
|dollars| timestampGMT| color|distinct_color_count_over_the_last_week|
+-------+--------------------+------+---------------------------------------+
| 17|2017-03-10 15:27:...|orange| 1|
| 13|2017-03-15 12:27:...| red| 2|
| 25|2017-03-18 11:27:...| red| 1|
+-------+--------------------+------+---------------------------------------+

最佳答案

编辑:

正如noleto在下面的回答中提到的那样,自pyspark 2.1以来,现在有一个大约可在窗口上运行的函数。

原始答案

我发现可以结合使用collect_set和size函数来模拟窗口上countCounttinct的功能:

from pyspark.sql.window import Window
from pyspark.sql import functions as F

#function to calculate number of seconds from number of days
days = lambda i: i * 86400

#create some test data
df = spark.createDataFrame([(17, "2017-03-10T15:27:18+00:00", "orange"),
(13, "2017-03-15T12:27:18+00:00", "red"),
(25, "2017-03-18T11:27:18+00:00", "red")],
["dollars", "timestampGMT", "color"])

#convert string timestamp to timestamp type
df = df.withColumn('timestampGMT', df.timestampGMT.cast('timestamp'))

#create window by casting timestamp to long (number of seconds)
w = (Window.orderBy(F.col("timestampGMT").cast('long')).rangeBetween(-days(7), 0))

#use collect_set and size functions to perform countDistinct over a window
df = df.withColumn('distinct_color_count_over_the_last_week', F.size(F.collect_set("color").over(w)))

df.show()

这导致记录的前一周有不同的颜色计数:
+-------+--------------------+------+---------------------------------------+
|dollars| timestampGMT| color|distinct_color_count_over_the_last_week|
+-------+--------------------+------+---------------------------------------+
| 17|2017-03-10 15:27:...|orange| 1|
| 13|2017-03-15 12:27:...| red| 2|
| 25|2017-03-18 11:27:...| red| 1|
+-------+--------------------+------+---------------------------------------+

关于count - pyspark:在一个窗口中计数不相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45869186/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com