From 504238e97d74b493c0ddeeaae7654c41129da69c Mon Sep 17 00:00:00 2001 From: Nimay Gupta <49588368+nimay-gupta@users.noreply.github.com> Date: Fri, 9 Apr 2021 01:20:54 +0530 Subject: [PATCH] Update stockreturns.py --- Lab7-Spark/stockreturns.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Lab7-Spark/stockreturns.py b/Lab7-Spark/stockreturns.py index 63b8539..70b134b 100644 --- a/Lab7-Spark/stockreturns.py +++ b/Lab7-Spark/stockreturns.py @@ -1,7 +1,6 @@ from pyspark.sql import SQLContext from pyspark import SparkContext -# other required imports here - + if __name__ == "__main__": # create Spark context with necessary configuration spark = SparkContext("local", "Stock Returns") @@ -10,9 +9,9 @@ df = SQLContext(spark).read.option("header", True).csv("stock_prices.csv") # calculate daily percentage returns - df = df.withColumn("return", 100*(df["close"]-df["open"])/df["open"]) + df = df.withColumn("return", ((df["close"]-df["open"])/df["open"])*100) - # average on company name + # average on date df = df.groupBy("date").avg().alias("avg_return") # save the average returns to output