Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
At this point please ensure you have completed the two previous yfinance and web scraping labs. In this assignment you will upload screenshots of your code and results. You will also be reviewing the submission for one of your peers and grading their work.
As a data scientist working for an investment firm, you will extract the revenue data for Tesla and GameStop and build a dashboard to compare the price of the stock vs the revenue.
Full Points: Working code that yields correct results
You will be graded on the dashboards displaying the specified data and the screenshots you took during the final project lab questions. There are 12 possible points for this assignment. Here is the breakdown:
Question 1 – Extracting Tesla Stock Data Using yfinance – 2 Points Question 2 – Extracting Tesla Revenue Data Using Webscraping – 1 Points Question 3 – Extracting GameStop Stock Data Using yfinance – 2 Points Question 4 – Extracting GameStop Revenue Data Using Webscraping – 1 Points Question 5 – Tesla Stock and Revenue Dashboard – 2 Points Question 6 – GameStop Stock and Revenue Dashboard- 2 Points Question 7 – Sharing your Assignment Notebook – 2 Points
For each problem points will be awarded as follows:
Full Points: Working code that yields correct results Partial Points: Partially correct code or results No Points: Did not attempt the problem or did not upload any solution
Here are some examples of a submission clearly showing both the Code and its Results/Output when executed from a Jupyter Notebook.
!pip install yfinance
#!pip install pandas
#!pip install requests
!pip install bs4
#!pip install plotly
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
#Define Graphing Function
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data.Date, infer_datetime_format=True), y=stock_data.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data.Date, infer_datetime_format=True), y=revenue_data.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
Ticker
function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA
.history
extract stock information and save it in a dataframe named tesla_data
. Set the period
parameter to max
so we get information for the maximum amount of time.reset_index(inplace=True)
function on the tesla_data
DataFrame and display the first five rows of the tesla_data
dataframe using the head
function.#1
tesla = yf.Ticker("TSLA")
#2
tesla_data = tesla.history(period="max")
#3
tesla_data.reset_index(inplace=True)
tesla_data.head()
requests
library to download the webpage https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue. Save the text of the response as a variable named html_data
.beautiful_soup
.Tesla Quarterly Revenue
and store it into a dataframe named tesla_revenue
. The dataframe should have columns Date
and Revenue
. Make sure the comma and dollar sign is removed from the Revenue
column.tesla_revenue
DataFrame to see if you have any.tesla_revenue
dataframe using the tail
function.#1
tesla_url = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue"
tesla_html_data = requests.get(tesla_url).text
#2
tesla_soup = BeautifulSoup(tesla_html_data, "html5lib")
#3
tesla_tables = tesla_soup.find_all('table')
for index,table in enumerate(tesla_tables):
if ("Tesla Quarterly Revenue" in str(table)):
tesla_table_index = index
tesla_revenue = pd.DataFrame(columns=["Date", "Revenue"])
for row in tesla_tables[tesla_table_index].tbody.find_all("tr"):
col = row.find_all("td")
if (col !=[]):
date = col[0].text
revenue = col[1].text.replace("$", "").replace(",", "")
tesla_revenue = tesla_revenue.append({"Date" : date, "Revenue" : revenue}, ignore_index=True)
#4
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
tesla_revenue
#5
tesla_revenue.tail()
Ticker
function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME
.history
extract stock information and save it in a dataframe named gme_data
. Set the period
parameter to max
so we get information for the maximum amount of time.reset_index(inplace=True)
function on the gme_data
DataFrame and display the first five rows of the gme_data
dataframe using the head
function.#1
gamestop = yf.Ticker("GME")
#2
gme_data = gamestop.history(period="max")
#3
gme_data.reset_index(inplace=True)
gme_data.head()
library
to download the webpage https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue. Save the text of the response as a variable named html_data
.beautiful_soup
.GameStop Quarterly Revenue
and store it into a dataframe named gme_revenue
. The dataframe should have columns Date
and Revenue
. Make sure the comma and dollar sign is removed from the Revenue
column using a method similar to what you did in Question 2.gme_revenue
dataframe using the tail
function.#1
gme_url = "https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue"
gme_html_data = requests.get(gme_url).text
#2
gme_soup = BeautifulSoup(gme_html_data, "html5lib")
#3
gme_tables = gme_soup.find_all('table')
for index,table in enumerate(gme_tables):
if ("GameStop Quarterly Revenue" in str(table)):
gme_table_index = index
gme_revenue = pd.DataFrame(columns=["Date", "Revenue"])
for row in gme_tables[gme_table_index].tbody.find_all("tr"):
col = row.find_all("td")
if (col !=[]):
date = col[0].text
revenue = col[1].text.replace("$", "").replace(",", "")
gme_revenue = gme_revenue.append({"Date" : date, "Revenue" : revenue}, ignore_index=True)
#4
gme_revenue.tail()
make_graph
function to graph the Tesla Stock Data, also provide a title for the graph.make_graph
function to graph the GameStop Stock Data, also provide a title for the graph.#1
make_graph(tesla_data, tesla_revenue, 'Tesla')
#2
make_graph(gme_data, gme_revenue, 'GameStop')
I hope this Peer-graded Assignment: Final Assignment Solution would be useful for you to learn something new from this Course. If it helped you then don’t forget to bookmark our site for more Quiz Answers.
This course is intended for audiences of all experiences who are interested in learning about new skills in a business context; there are no prerequisite courses.
Keep Learning!
More Peer-graded Assignment Solutions >>