I want to create a column in pyspark that refers upon itself after the first row.
Customer | Week | Price | Index change | Column to be created
A 1 10 0.5 10
A 2 13 0.1 10* (1+0.1) = 11
A 3 16 0.6 11* (1+0.6) = 17.6
A 4 16 0.1 17.6 * (1+0.1) = 19.36
There are multiple customers in this dataset, each with 52 weeks. I know I have to use a window function, but I am having trouble applying that along with creating a function that essentially refers to itself after the first row, which refers to another column. I feel like it should be something like the below but not sure how to make it work and if you can refer to a column while it's being created?
df = df.withColumn('Column to be created',
F.when(F.col('week') != 1,
lag(df['Column to be created'])*(1+df['Index change']).over(win))
.otherwise(F.col('Price')))
* win refers to a partitionby that I have created already