0% found this document useful (0 votes)
2 views7 pages

Data Science Project

The document outlines a series of exercises for extracting and visualizing stock and revenue data for Tesla and GameStop using Python libraries such as yfinance, BeautifulSoup, and Plotly. It includes steps for installing dependencies, fetching historical stock data, web scraping revenue data, and generating interactive dashboards to display the information. The final output consists of visualizations for both Tesla and GameStop, showcasing their historical share prices and revenue growth.

Uploaded by

Akhil Kilari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Data Science Project

The document outlines a series of exercises for extracting and visualizing stock and revenue data for Tesla and GameStop using Python libraries such as yfinance, BeautifulSoup, and Plotly. It includes steps for installing dependencies, fetching historical stock data, web scraping revenue data, and generating interactive dashboards to display the information. The final output consists of visualizations for both Tesla and GameStop, showcasing their historical share prices and revenue growth.

Uploaded by

Akhil Kilari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#

=====================================================================
=========
# STEP 0: Install Dependencies and Define Dashboard Utility Function
#
=====================================================================
=========
!pip install yfinance bs4 plotly nbformat --quiet

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 warnings

# Disable standard evaluation warnings for clean presentation


[Link]("ignore", category=FutureWarning)

def make_graph(stock_data, revenue_data, stock_name):


"""
Generates an interactive web dashboard showcasing historical trends
by aligning share price tracking side-by-side with revenue growth data.
"""
fig = make_subplots(
rows=2, cols=1,
shared_xaxes=True,
subplot_titles=("Historical Share Price", "Historical Revenue"),
vertical_spacing=0.3
)
# Cast/clean datetime series safely for dynamic plotting layout
stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']

# Add Price Track Trace


fig.add_trace(
[Link](
x=pd.to_datetime(stock_data_specific.Date),
y=stock_data_specific.[Link]("float"),
name="Share Price ($)"
),
row=1, col=1
)

# Add Revenue Track Trace


fig.add_trace(
[Link](
x=pd.to_datetime(revenue_data_specific.Date),
y=revenue_data_specific.[Link]("float"),
name="Revenue ($M)"
),
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=600,
title=f"{stock_name} Data Analytics Dashboard",
xaxis_rangeslider_visible=True
)
[Link]()

#
=====================================================================
=========
# EXERCISE 1.2: Extract Tesla Stock Data Using yfinance
#
=====================================================================
=========
# Initialize yfinance object for Tesla
tesla_ticker = [Link]("TSLA")

# Extract complete max historical timeline data


tesla_data = tesla_ticker.history(period="max")

# Reset DataFrame index to make Date a regular column


tesla_data.reset_index(inplace=True)

# Display the first five rows for grading evaluation


print("--- Exercise 1.2: Tesla Stock Data (First 5 Rows) ---")
print(tesla_data.head())

#
=====================================================================
=========
# EXERCISE 1.3: Web Scrape and Parse Tesla Revenue Data
#
=====================================================================
=========
# Fetch the historical static snapshot of the target web portal data
tesla_url = "[Link]
IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/[Link]"
tesla_html = [Link](tesla_url).text

# Parse HTML structures natively


soup_tesla = BeautifulSoup(tesla_html, "[Link]")

# Target and construct the raw table records


tesla_revenue_list = []
for row in soup_tesla.find_all("tbody")[1].find_all("tr"):
cols = row.find_all("td")
if len(cols) >= 2:
date = cols[0].[Link]()
revenue = cols[1].[Link]()
tesla_revenue_list.append({"Date": date, "Revenue": revenue})

tesla_revenue = [Link](tesla_revenue_list)

# Data Cleaning: Strip symbol markers and engineering currency notation


tesla_revenue["Revenue"] = tesla_revenue['Revenue'].[Link](r'[\$,]', '', regex=True)

# Drop missing values and records with missing string elements


tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]

# Display the last five rows for grading evaluation


print("\n--- Exercise 1.3: Tesla Revenue Data (Last 5 Rows) ---")
print(tesla_revenue.tail())

#
=====================================================================
=========
# EXERCISE 1.4: Extract GameStop Stock Data Using yfinance
#
=====================================================================
=========
# Initialize yfinance object for GameStop
gme_ticker = [Link]("GME")

# Extract complete historical dataframe properties


gme_data = gme_ticker.history(period="max")

# Reset index structures cleanly


gme_data.reset_index(inplace=True)

# Display the first five rows for grading evaluation


print("\n--- Exercise 1.4: GameStop Stock Data (First 5 Rows) ---")
print(gme_data.head())

#
=====================================================================
=========
# EXERCISE 1.5: Web Scrape and Parse GameStop Revenue Data
#
=====================================================================
=========
# Fetch static target mirror for GameStop financials
gme_url = "[Link]
IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/
automated_sharing_webmarking.html"
gme_html = [Link](gme_url).text

# Process content via BeautifulSoup tree structures


soup_gme = BeautifulSoup(gme_html, "[Link]")

gme_revenue_list = []
for row in soup_gme.find_all("tbody")[1].find_all("tr"):
cols = row.find_all("td")
if len(cols) >= 2:
date = cols[0].[Link]()
revenue = cols[1].[Link]()
gme_revenue_list.append({"Date": date, "Revenue": revenue})

gme_revenue = [Link](gme_revenue_list)

# Data Cleaning: Clean formatting configurations across standard types


gme_revenue["Revenue"] = gme_revenue['Revenue'].[Link](r'[\$,]', '', regex=True)
gme_revenue.dropna(inplace=True)
gme_revenue = gme_revenue[gme_revenue['Revenue'] != ""]

# Display the last five rows for grading evaluation


print("\n--- Exercise 1.5: GameStop Revenue Data (Last 5 Rows) ---")
print(gme_revenue.tail())

#
=====================================================================
=========
# EXERCISE 1.6: Plot Tesla Dashboard
#
=====================================================================
=========
print("\nGenerating Tesla Dashboard Graph...")
make_graph(tesla_data, tesla_revenue, 'Tesla')

#
=====================================================================
=========
# EXERCISE 1.7: Plot GameStop Dashboard
#
=====================================================================
=========
print("\nGenerating GameStop Dashboard Graph...")
make_graph(gme_data, gme_revenue, 'GameStop')

You might also like