My goal is to calculate another column, keeping the same number of rows as the original DataFrame, where I can show the mean balance for each user for the last 30 days.
I guess it can be done using Window Functions, partitioning by user and somehow limiting the rows which are between the current date and 30 days before, but I don't know how to implement it in PySpark.
I have the following Spark DataFrame:
| userId | date | balance |
|---|---|---|
| A | 09/06/2020 | 100 |
| A | 03/07/2020 | 200 |
| A | 05/08/2020 | 600 |
| A | 30/08/2020 | 1000 |
| A | 15/09/2020 | 500 |
| B | 03/01/2020 | 100 |
| B | 05/04/2020 | 200 |
| B | 29/04/2020 | 600 |
| B | 01/05/2020 | 1600 |
My desired output DataFrame would be:
| userId | date | balance | mean_last_30days_balance |
|---|---|---|---|
| A | 09/06/2020 | 100 | 100 |
| A | 03/07/2020 | 200 | 150 |
| A | 05/08/2020 | 600 | 600 |
| A | 30/08/2020 | 1000 | 800 |
| A | 15/09/2020 | 500 | 750 |
| B | 03/01/2020 | 100 | 100 |
| B | 05/04/2020 | 200 | 200 |
| B | 29/04/2020 | 600 | 400 |
| B | 01/05/2020 | 1600 | 800 |
from datetime import datetime
from pyspark.sql import types as T
data = [("A",datetime.strptime("09/06/2020",'%d/%m/%Y'),100),
("A",datetime.strptime("03/07/2020",'%d/%m/%Y'),200),
("A",datetime.strptime("05/08/2020",'%d/%m/%Y'),600),
("A",datetime.strptime("30/08/2020",'%d/%m/%Y'),1000),
("A",datetime.strptime("15/09/2020",'%d/%m/%Y'),500),
("B",datetime.strptime("03/01/2020",'%d/%m/%Y'),100),
("B",datetime.strptime("05/04/2020",'%d/%m/%Y'),200),
("B",datetime.strptime("29/04/2020",'%d/%m/%Y'),600),
("B",datetime.strptime("01/05/2020",'%d/%m/%Y'),1600)]
schema = T.StructType([T.StructField("userId",T.StringType(),True),
T.StructField("date",T.DateType(),True),
T.StructField("balance",T.StringType(),True)
])
sdf_prueba = spark.createDataFrame(data=data,schema=schema)
sdf_prueba.printSchema()
sdf_prueba.orderBy(F.col('userId').asc(),F.col('date').asc()).show(truncate=False)