Skip to content

Commit

Permalink
Update stockreturns.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nimay-gupta authored Apr 8, 2021
1 parent 9d3410f commit 504238e
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Lab7-Spark/stockreturns.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 504238e

Please sign in to comment.