Extracting essential data from a dataset and displaying it is a necessary part of data science;
therefore individuals can make correct decisions based on the data. In this assignment, you will
extract some stock data, you will then display this data in a graph.
Note:- If you are working Locally using anaconda, please uncomment the following code and
execute it. Use the version as per your python version.
!pip install yfinance
!pip install bs4
!pip install nbformat
!pip install --upgrade plotly
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from [Link] import make_subplots
import [Link] as pio
[Link] = "iframe"
In Python, you can ignore warnings using the warnings module. You can use the filterwarnings
function to filter or ignore specific warning messages or categories.
import warnings
# Ignore all warnings
[Link]("ignore", category=FutureWarning)
Define Graphing Function
In this section, we define the function make_graph. You don't have to know how the function
works, you should only care about the inputs. It takes a dataframe with stock data (dataframe
must contain Date and Close columns), a dataframe with revenue data (dataframe must
contain Date and Revenue columns), and the name of the stock.
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)
stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-
04-30']
fig.add_trace([Link](x=pd.to_datetime(stock_data_specific.Date,
infer_datetime_format=True),
y=stock_data_specific.[Link]("float"), name="Share Price"),
row=1, col=1)
fig.add_trace([Link](x=pd.to_datetime(revenue_data_specific.Date,
infer_datetime_format=True),
y=revenue_data_specific.[Link]("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)
[Link]()
from [Link] import display, HTML
fig_html = fig.to_html()
display(HTML(fig_html))
Use the make_graph function that we’ve already defined. You’ll need to invoke it in questions 5
and 6 to display the graphs and create the dashboard.
Note: You don’t need to redefine the function for plotting graphs anywhere else in
this notebook; just use the existing function.
Question 1: Use yfinance to Extract Stock Data
Using the 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.
Using the ticker object and the function 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 the index using the 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. Take a screenshot of the results and code from the beginning of Question 1 to the
results below.
Question 2: Use Webscraping to Extract Tesla Revenue
Data
Use the requests library to download the webpage [Link]
[Link]/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/
[Link] Save the text of the response as a variable named html_data.
Parse the html data using beautiful_soup using parser i.e html5lib or [Link].
Using BeautifulSoup or the read_html function extract the table with Tesla Revenue and
store it into a dataframe named tesla_revenue. The dataframe should have columns Date
and Revenue.
Execute the following line to remove the comma and dollar sign from the Revenue column.
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].[Link](',|\
$',"",regex=True)
Execute the following lines to remove an null or empty strings in the Revenue column.
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a
screenshot of the results.
Question 3: Use yfinance to Extract Stock Data
Using the 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.
Using the ticker object and the function 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 the index using the 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.
Take a screenshot of the results and code from the beginning of Question 3 to the results below.
Question 4: Use Webscraping to Extract GME Revenue
Data
Use the requests library to download the webpage [Link]
[Link]/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/
[Link]. Save the text of the response as a variable named html_data_2.
Parse the html data using beautiful_soup using parser i.e html5lib or [Link].
Using BeautifulSoup or the read_html function extract the table with GameStop 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.
Note: Use the method similar to what you did in question 2.
Display the last five rows of the gme_revenue dataframe using the tail function. Take a
screenshot of the results.
Question 5: Plot Tesla Stock Graph
Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph.
Note the graph will only show data upto June 2021.
Question 6: Plot GameStop Stock Graph
Use the make_graph function to graph the GameStop Stock Data, also provide a title for the
graph. The structure to call the make_graph function is make_graph(gme_data,
gme_revenue, 'GameStop'). Note the graph will only show data upto June 2021.
Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using
machine learning, signal processing, and computer vision to determine how videos impact
human cognition. Joseph has been working for IBM since he completed his PhD.
Azim Hirjani
Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
2022-02-28 1.2 Lakshmi Holla Changed the URL of GameStop
2020-11-10 1.1 Malika Singla Deleted the Optional part
2020-08-27 1.0 Malika Singla Added lab to GitLab
© IBM Corporation 2020. All rights reserved.