I use pySpark and set up my data frame with two columns representing the daily price of assets as follows:
ind = sc.parallelize(range(1,5)) prices = sc.parallelize([33.3,31.1,51.2,21.3]) data = ind.zip(prices) df = sqlCtx.createDataFrame(data,["day","price"])
I get after applying df.show() :
+---+-----+ |day|price| +---+-----+ | 1| 33.3| | 2| 31.1| | 3| 51.2| | 4| 21.3| +---+-----+
Which is good and all. I would like to have another column that contains daily returns of the price column, i.e. something like
(price(day2)-price(day1))/(price(day1))
After much research, I was told that this is most effectively achieved by using the pyspark.sql.window functions, but I cannot figure out how to do this.
source share