這是我當前的資料集:
from pyspark.sql import Window
import pyspark.sql.functions as psf
df = spark.createDataFrame([("2","1",1),
("3","2",2)],
schema = StructType([StructField("Data", StringType()),
StructField("Source",StringType()),
StructField("Date", IntegerType())]))
display(df.withColumn("Result",psf.collect_set("Data").over(Window.partitionBy("Source").orderBy("Date"))))
輸出:
| 資料 | 來源 | 日期 | 結果 |
|---|---|---|---|
| 2 | 1 | 1 | [“2”] |
| 3 | 1 | 2 | ["2","3"] |
為什么在 Window 上使用 collect_set 函式時,我3在第一行的列中缺少值?Resultordered
我也嘗試過使用collect_list,但我得到了相同的結果。
我想要的輸出是:
| 資料 | 來源 | 日期 | 結果 |
|---|---|---|---|
| 2 | 1 | 1 | ["2","3"] |
| 3 | 1 | 2 | ["2","3"] |
其中值的順序Result被保留 - 第一個是 whereDate = 1而第二個是Date = 2
uj5u.com熱心網友回復:
您需要使用帶有unboundedPrecedingand的 Window Window.unboundedFollowing:
Window.partitionBy("Source").orderBy("Date") \
.rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing)
默認情況下,rowsBetween(Window.unboundedPreceding, Window.currentRow)當你有一個orderBy
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/405141.html
標籤:
