![](/img/trans.png)
[英]Count key value that matches certain value in pyspark dataframe using udf
[英]How to count the number of adjacent values in a Pyspark Dataframe equal to a certain value using a rolling window function?
可以使用以下方法創建示例數據框:
from pyspark.sql.functions import col
from pyspark.sql.window import Window
df = sc.parallelize([['2019-08-29 01:00:00',0],
['2019-08-29 02:00:00',0],
['2019-08-29 03:00:00',0],
['2019-08-29 04:00:00',1],
['2019-08-29 05:00:00',2],
['2019-08-29 06:00:00',3],
['2019-08-29 07:00:00',0],
['2019-08-29 08:00:00',2],
['2019-08-29 09:00:00',0],
['2019-08-29 10:00:00',1]]).toDF(['DATETIME','VAL']).withColumn('DATETIME',col('DATETIME').cast('timestamp'))
我想生成一個列,其計數等於 3 小時內(當前時間的 +/- 1 小時,包括當前 Val)出現 0 值的次數。 可以使用以下方法創建窗口:
w1 = (Window()
.orderBy(col('DATETIME').cast('long'))
.rangeBetween(-(60*60), 60*60))
期望的結果:
+-------------------+---+---+
| DATETIME|VAL|NUM|
+-------------------+---+---+
|2019-08-29 01:00:00| 0| 2|
|2019-08-29 02:00:00| 0| 3|
|2019-08-29 03:00:00| 0| 2|
|2019-08-29 04:00:00| 1| 1|
|2019-08-29 05:00:00| 2| 0|
|2019-08-29 06:00:00| 3| 1|
|2019-08-29 07:00:00| 0| 1|
|2019-08-29 08:00:00| 2| 2|
|2019-08-29 09:00:00| 0| 1|
|2019-08-29 10:00:00| 1| 1|
+-------------------+---+---+
如果每個DATETIME只有 1 個條目,則可以使用超前和滯后功能來獲取上一個和下一個值,然后您可以計數為零。
from pyspark.sql.functions import udf, array, col
from pyspark.sql.types import IntegerType
count_zeros_udf = udf(lambda arr: arr.count(0), IntegerType())
df.withColumn('lag1', f.lag(col('VAL'), 1, -1).over(Window.orderBy("DATETIME"))) # Get the previous value
.withColumn('lag2', f.lead(col('VAL'), 1, -1).over(Window.orderBy("DATETIME"))) # Get the next value
.withColumn('NUM', count_zeros_udf(array('VAL', 'lag1', 'lag2'))) # Count zeros using the udf
.drop('lag1', 'lag2') # Drop the extra columns
.show()
+-------------------+---+---+
| DATETIME|VAL|NUM|
+-------------------+---+---+
|2019-08-29 01:00:00| 0| 2|
|2019-08-29 02:00:00| 0| 3|
|2019-08-29 03:00:00| 0| 2|
|2019-08-29 04:00:00| 1| 1|
|2019-08-29 05:00:00| 2| 0|
|2019-08-29 06:00:00| 3| 1|
|2019-08-29 07:00:00| 0| 1|
|2019-08-29 08:00:00| 2| 2|
|2019-08-29 09:00:00| 0| 1|
|2019-08-29 10:00:00| 1| 1|
+-------------------+---+---+
使用pyspark >= 2.4 ,您可以在窗口上使用帶有Pandas UDF 的UDF ,如下所述用戶定義的函數要應用於 PySpark 中的窗口? . 不幸的是,我沒有 pyspark 2.4 或更高版本,因此我無法對其進行測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.