С помощью приведенного ниже набора данных pyspark (2.1), как использовать оконную функцию, которая будет подсчитывать количество раз, когда день недели текущей записи появлялся в течение последних 28 дней.
Пример кадра данных:
from pyspark.sql import functions as F
df = sqlContext.createDataFrame([
("a", "1", "2018-01-01 12:01:01","Monday"),
("a", "13", "2018-01-01 14:01:01","Monday"),
("a", "22", "2018-01-02 22:01:01","Tuesday"),
("a", "43", "2018-01-08 01:01:01","Monday"),
("a", "43", "2018-01-09 01:01:01","Tuesday"),
("a", "74", "2018-01-10 12:01:01","Wednesday"),
("a", "95", "2018-01-15 06:01:01","Monday"),
], ["person_id", "other_id", "timestamp","dow"])
df.withColumn("dow_count",`some window function`)
Возможное окно
from pyspark.sql import Window
from pyspark.sql import functions as F
Days_28 = (86400 * 28)
window= Window.partitionBy("person_id").orderBy('timestamp').rangeBetween(-Days_30, -1)
## I know this next line is wrong
df.withColumn("dow_count",F.sum(F.when(Current_day=windowed_day,1).otherwise(0)).over(window))
Пример вывода
df.show()
+---------+--------+-------------------+---------+---------+
|person_id|other_id| timestamp| dow|dow_count|
+---------+--------+-------------------+---------+---------+
| a| 1|2018-01-01 12:01:01| Monday|0 |
| a| 13|2018-01-01 14:01:01| Monday|1 |
| a| 22|2018-01-02 22:01:01| Tuesday|0 |
| a| 43|2018-01-08 01:01:01| Monday|2 |
| a| 43|2018-01-09 01:01:01| Tuesday|1 |
| a| 74|2018-01-10 12:01:01|Wednesday|0 |
| a| 95|2018-01-15 06:01:01| Monday|3 |
+---------+--------+-------------------+---------+---------+