Created
October 27, 2022 09:59
-
-
Save Faiza-K/9c1eb04b9a633018ec36bfddfd1590e8 to your computer and use it in GitHub Desktop.
Extracting and Visualizing Stock Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "<center>\n <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n</center>\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "<h1>Extracting and Visualizing Stock Data</h1>\n<h2>Description</h2>\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "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.\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "<h2>Table of Contents</h2>\n<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n <ul>\n <li>Define a Function that Makes a Graph</li>\n <li>Question 1: Use yfinance to Extract Stock Data</li>\n <li>Question 2: Use Webscraping to Extract Tesla Revenue Data</li>\n <li>Question 3: Use yfinance to Extract Stock Data</li>\n <li>Question 4: Use Webscraping to Extract GME Revenue Data</li>\n <li>Question 5: Plot Tesla Stock Graph</li>\n <li>Question 6: Plot GameStop Stock Graph</li>\n </ul>\n<p>\n Estimated Time Needed: <strong>30 min</strong></p>\n</div>\n\n<hr>\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": "Collecting yfinance\n Downloading yfinance-0.1.66-py2.py3-none-any.whl (25 kB)\nRequirement already satisfied: requests>=2.20 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from yfinance) (2.25.1)\nRequirement already satisfied: lxml>=4.5.1 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from yfinance) (4.6.3)\nCollecting multitasking>=0.0.7\n Downloading multitasking-0.0.10.tar.gz (8.2 kB)\nRequirement already satisfied: pandas>=0.24 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from yfinance) (1.2.4)\nRequirement already satisfied: numpy>=1.15 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from yfinance) (1.19.2)\nRequirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from pandas>=0.24->yfinance) (2.8.1)\nRequirement already satisfied: pytz>=2017.3 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from pandas>=0.24->yfinance) (2021.1)\nRequirement already satisfied: six>=1.5 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance) (1.15.0)\nRequirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from requests>=2.20->yfinance) (2021.10.8)\nRequirement already satisfied: chardet<5,>=3.0.2 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from requests>=2.20->yfinance) (3.0.4)\nRequirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from requests>=2.20->yfinance) (1.26.6)\nRequirement already satisfied: idna<3,>=2.5 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from requests>=2.20->yfinance) (2.8)\nBuilding wheels for collected packages: multitasking\n Building wheel for multitasking (setup.py) ... \u001b[?25ldone\n\u001b[?25h Created wheel for multitasking: filename=multitasking-0.0.10-py3-none-any.whl size=8487 sha256=ea9a94a8cc65c6d3b2c3bc32dde8fad87970633dfbc7f1e53e1b56504a700cdc\n Stored in directory: /tmp/wsuser/.cache/pip/wheels/21/c9/66/b41c847de65c7985db52ec21d59996841598b8b0e93f2b9500\nSuccessfully built multitasking\nInstalling collected packages: multitasking, yfinance\nSuccessfully installed multitasking-0.0.10 yfinance-0.1.66\nCollecting bs4\n Downloading bs4-0.0.1.tar.gz (1.1 kB)\nRequirement already satisfied: beautifulsoup4 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from bs4) (4.9.3)\nRequirement already satisfied: soupsieve>1.2 in /opt/conda/envs/Python-3.8-main/lib/python3.8/site-packages (from beautifulsoup4->bs4) (2.2.1)\nBuilding wheels for collected packages: bs4\n Building wheel for bs4 (setup.py) ... \u001b[?25ldone\n\u001b[?25h Created wheel for bs4: filename=bs4-0.0.1-py3-none-any.whl size=1273 sha256=717bd20f1a7d3b5f192ba92f04b1d92316ba89877636902b3b3ff5df3addab96\n Stored in directory: /tmp/wsuser/.cache/pip/wheels/75/78/21/68b124549c9bdc94f822c02fb9aa3578a669843f9767776bca\nSuccessfully built bs4\nInstalling collected packages: bs4\nSuccessfully installed bs4-0.0.1\n" | |
| } | |
| ], | |
| "source": "!pip install yfinance\n#!pip install pandas\n#!pip install requests\n!pip install bs4\n#!pip install plotly" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "import yfinance as yf\nimport pandas as pd\nimport requests\nfrom bs4 import BeautifulSoup\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "## Define Graphing Function\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "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.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "def make_graph(stock_data, revenue_data, stock):\n fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=(\"Historical Share Price\", \"Historical Revenue\"), vertical_spacing = .3)\n stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']\n revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']\n fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype(\"float\"), name=\"Share Price\"), row=1, col=1)\n fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype(\"float\"), name=\"Revenue\"), row=2, col=1)\n fig.update_xaxes(title_text=\"Date\", row=1, col=1)\n fig.update_xaxes(title_text=\"Date\", row=2, col=1)\n fig.update_yaxes(title_text=\"Price ($US)\", row=1, col=1)\n fig.update_yaxes(title_text=\"Revenue ($US Millions)\", row=2, col=1)\n fig.update_layout(showlegend=False,\n height=900,\n title=stock,\n xaxis_rangeslider_visible=True)\n fig.show()" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "## Question 1: Use yfinance to Extract Stock Data\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "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`.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "tesla=yf.Ticker('TSLA')" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "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.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "tesla_data= tesla.history(period= \"max\")\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "**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.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Date</th>\n <th>Open</th>\n <th>High</th>\n <th>Low</th>\n <th>Close</th>\n <th>Volume</th>\n <th>Dividends</th>\n <th>Stock Splits</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>2010-06-29</td>\n <td>3.800</td>\n <td>5.000</td>\n <td>3.508</td>\n <td>4.778</td>\n <td>93831500</td>\n <td>0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>2010-06-30</td>\n <td>5.158</td>\n <td>6.084</td>\n <td>4.660</td>\n <td>4.766</td>\n <td>85935500</td>\n <td>0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>2010-07-01</td>\n <td>5.000</td>\n <td>5.184</td>\n <td>4.054</td>\n <td>4.392</td>\n <td>41094000</td>\n <td>0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>2010-07-02</td>\n <td>4.600</td>\n <td>4.620</td>\n <td>3.742</td>\n <td>3.840</td>\n <td>25699000</td>\n <td>0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>4</th>\n <td>2010-07-06</td>\n <td>4.000</td>\n <td>4.000</td>\n <td>3.166</td>\n <td>3.222</td>\n <td>34334500</td>\n <td>0</td>\n <td>0.0</td>\n </tr>\n </tbody>\n</table>\n</div>", | |
| "text/plain": " Date Open High Low Close Volume Dividends Stock Splits\n0 2010-06-29 3.800 5.000 3.508 4.778 93831500 0 0.0\n1 2010-06-30 5.158 6.084 4.660 4.766 85935500 0 0.0\n2 2010-07-01 5.000 5.184 4.054 4.392 41094000 0 0.0\n3 2010-07-02 4.600 4.620 3.742 3.840 25699000 0 0.0\n4 2010-07-06 4.000 4.000 3.166 3.222 34334500 0 0.0" | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": "tesla_data.reset_index(inplace= True)\ntesla_data.head()" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "## Question 2: Use Webscraping to Extract Tesla Revenue Data\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Use the `requests` library to download the webpage [https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue](https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0220ENSkillsNetwork23455606-2021-01-01). Save the text of the response as a variable named `html_data`.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "url= \"https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue\"\nhtml_data= requests.get(url).text\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Parse the html data using `beautiful_soup`.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "soup= BeautifulSoup(html_data, 'html5lib')" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Using `BeautifulSoup` or the `read_html` function extract the table with `Tesla Quarterly Revenue` and store it into a dataframe named `tesla_revenue`. The dataframe should have columns `Date` and `Revenue`.\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "<details><summary>Click here if you need help locating the table</summary>\n\n```\n \nBelow is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab\n \nsoup.find_all(\"tbody\")[1]\n \nIf you want to use the read_html function the table is located at index 1\n\n\n```\n\n</details>\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "tesla_revenue= pd.DataFrame(columns= ['Date', 'Revenue'])\n\ntables= soup.find_all('table')\n\nfor index, table in enumerate(tables):\n if (\"Tesla Quarterly Revenue\" in str(table)):\n table_index = index\n for row in tables[table_index].tbody.find_all(\"tr\"):\n col = row.find_all(\"td\")\n date = col[0].text\n rev = col[1].text\n tesla_revenue = tesla_revenue.append({\"Date\":date,\"Revenue\":rev}, ignore_index=True)" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Execute the following line to remove the comma and dollar sign from the `Revenue` column.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": "<ipython-input-10-2aef5327de36>:1: FutureWarning: The default value of regex will change from True to False in a future version.\n tesla_revenue[\"Revenue\"] = tesla_revenue['Revenue'].str.replace(',|\\$',\"\")\n" | |
| } | |
| ], | |
| "source": "tesla_revenue[\"Revenue\"] = tesla_revenue['Revenue'].str.replace(',|\\$',\"\")" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Execute the following lines to remove an null or empty strings in the Revenue column.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "tesla_revenue.dropna(inplace=True)\n\ntesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != \"\"]" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Display the last 5 row of the `tesla_revenue` dataframe using the `tail` function. Take a screenshot of the results.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Date</th>\n <th>Revenue</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>44</th>\n <td>2010-09-30</td>\n <td>31</td>\n </tr>\n <tr>\n <th>45</th>\n <td>2010-06-30</td>\n <td>28</td>\n </tr>\n <tr>\n <th>46</th>\n <td>2010-03-31</td>\n <td>21</td>\n </tr>\n <tr>\n <th>48</th>\n <td>2009-09-30</td>\n <td>46</td>\n </tr>\n <tr>\n <th>49</th>\n <td>2009-06-30</td>\n <td>27</td>\n </tr>\n </tbody>\n</table>\n</div>", | |
| "text/plain": " Date Revenue\n44 2010-09-30 31\n45 2010-06-30 28\n46 2010-03-31 21\n48 2009-09-30 46\n49 2009-06-30 27" | |
| }, | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": "tesla_revenue.tail(5)" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "## Question 3: Use yfinance to Extract Stock Data\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "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`.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "game_stop= yf.Ticker(\"GME\")" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "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.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "gme_data= game_stop.history(period= \"max\")" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "**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.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Date</th>\n <th>Open</th>\n <th>High</th>\n <th>Low</th>\n <th>Close</th>\n <th>Volume</th>\n <th>Dividends</th>\n <th>Stock Splits</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>2002-02-13</td>\n <td>6.480514</td>\n <td>6.773400</td>\n <td>6.413183</td>\n <td>6.766666</td>\n <td>19054000</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>2002-02-14</td>\n <td>6.850828</td>\n <td>6.864294</td>\n <td>6.682503</td>\n <td>6.733001</td>\n <td>2755400</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>2002-02-15</td>\n <td>6.733002</td>\n <td>6.749834</td>\n <td>6.632007</td>\n <td>6.699337</td>\n <td>2097400</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>2002-02-19</td>\n <td>6.665670</td>\n <td>6.665670</td>\n <td>6.312188</td>\n <td>6.430016</td>\n <td>1852600</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>4</th>\n <td>2002-02-20</td>\n <td>6.463681</td>\n <td>6.648839</td>\n <td>6.413183</td>\n <td>6.648839</td>\n <td>1723200</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>4969</th>\n <td>2021-11-08</td>\n <td>212.500000</td>\n <td>225.039993</td>\n <td>212.009995</td>\n <td>218.639999</td>\n <td>2157900</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>4970</th>\n <td>2021-11-09</td>\n <td>222.000000</td>\n <td>223.490005</td>\n <td>204.440002</td>\n <td>206.600006</td>\n <td>1845300</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>4971</th>\n <td>2021-11-10</td>\n <td>202.119995</td>\n <td>207.389999</td>\n <td>196.500000</td>\n <td>199.190002</td>\n <td>1309400</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>4972</th>\n <td>2021-11-11</td>\n <td>200.000000</td>\n <td>208.089996</td>\n <td>198.149994</td>\n <td>204.320007</td>\n <td>1079400</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n <tr>\n <th>4973</th>\n <td>2021-11-12</td>\n <td>202.139999</td>\n <td>205.360001</td>\n <td>197.880005</td>\n <td>202.100006</td>\n <td>926700</td>\n <td>0.0</td>\n <td>0.0</td>\n </tr>\n </tbody>\n</table>\n<p>4974 rows \u00d7 8 columns</p>\n</div>", | |
| "text/plain": " Date Open High Low Close Volume \\\n0 2002-02-13 6.480514 6.773400 6.413183 6.766666 19054000 \n1 2002-02-14 6.850828 6.864294 6.682503 6.733001 2755400 \n2 2002-02-15 6.733002 6.749834 6.632007 6.699337 2097400 \n3 2002-02-19 6.665670 6.665670 6.312188 6.430016 1852600 \n4 2002-02-20 6.463681 6.648839 6.413183 6.648839 1723200 \n... ... ... ... ... ... ... \n4969 2021-11-08 212.500000 225.039993 212.009995 218.639999 2157900 \n4970 2021-11-09 222.000000 223.490005 204.440002 206.600006 1845300 \n4971 2021-11-10 202.119995 207.389999 196.500000 199.190002 1309400 \n4972 2021-11-11 200.000000 208.089996 198.149994 204.320007 1079400 \n4973 2021-11-12 202.139999 205.360001 197.880005 202.100006 926700 \n\n Dividends Stock Splits \n0 0.0 0.0 \n1 0.0 0.0 \n2 0.0 0.0 \n3 0.0 0.0 \n4 0.0 0.0 \n... ... ... \n4969 0.0 0.0 \n4970 0.0 0.0 \n4971 0.0 0.0 \n4972 0.0 0.0 \n4973 0.0 0.0 \n\n[4974 rows x 8 columns]" | |
| }, | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": "gme_data.reset_index(inplace = True)\ngme_data" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "## Question 4: Use Webscraping to Extract GME Revenue Data\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Use the `requests` library to download the webpage [https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue](https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0220ENSkillsNetwork23455606-2021-01-01). Save the text of the response as a variable named `html_data`.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "url = \"https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue\"\nhtml_data = requests.get(url).text" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Parse the html data using `beautiful_soup`.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "soup1=BeautifulSoup(html_data,'html5lib')\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Using `BeautifulSoup` or the `read_html` function extract the table with `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.\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "<details><summary>Click here if you need help locating the table</summary>\n\n```\n \nBelow is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab\n \nsoup.find_all(\"tbody\")[1]\n \nIf you want to use the read_html function the table is located at index 1\n\n\n```\n\n</details>\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": "gme_revenue= pd.DataFrame(columns= ['Date', 'Revenue'])\n\ntables = soup1.find_all('table')\n\nfor index, table in enumerate (tables):\n if ('GameStop Quarterly Revenue' in str(table)):\n for row in table.find('tbody').find_all('tr'):\n col= row.find_all('td')\n date= col[0].text\n rev= col[1].text\n \n gme_revenue= gme_revenue.append({\"Date\": date, \"Revenue\": rev}, ignore_index= True)" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Display the last five rows of the `gme_revenue` dataframe using the `tail` function. Take a screenshot of the results.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Date</th>\n <th>Revenue</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>62</th>\n <td>2006-01-31</td>\n <td>$1,667</td>\n </tr>\n <tr>\n <th>63</th>\n <td>2005-10-31</td>\n <td>$534</td>\n </tr>\n <tr>\n <th>64</th>\n <td>2005-07-31</td>\n <td>$416</td>\n </tr>\n <tr>\n <th>65</th>\n <td>2005-04-30</td>\n <td>$475</td>\n </tr>\n <tr>\n <th>66</th>\n <td>2005-01-31</td>\n <td>$709</td>\n </tr>\n </tbody>\n</table>\n</div>", | |
| "text/plain": " Date Revenue\n62 2006-01-31 $1,667\n63 2005-10-31 $534\n64 2005-07-31 $416\n65 2005-04-30 $475\n66 2005-01-31 $709" | |
| }, | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": "gme_revenue.tail(5)" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": "<ipython-input-20-f03406dd5959>:1: FutureWarning: The default value of regex will change from True to False in a future version.\n gme_revenue[\"Revenue\"] = gme_revenue['Revenue'].str.replace(',|\\$',\"\")\n" | |
| } | |
| ], | |
| "source": "gme_revenue[\"Revenue\"] = gme_revenue['Revenue'].str.replace(',|\\$',\"\")" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Date</th>\n <th>Revenue</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>62</th>\n <td>2006-01-31</td>\n <td>1667</td>\n </tr>\n <tr>\n <th>63</th>\n <td>2005-10-31</td>\n <td>534</td>\n </tr>\n <tr>\n <th>64</th>\n <td>2005-07-31</td>\n <td>416</td>\n </tr>\n <tr>\n <th>65</th>\n <td>2005-04-30</td>\n <td>475</td>\n </tr>\n <tr>\n <th>66</th>\n <td>2005-01-31</td>\n <td>709</td>\n </tr>\n </tbody>\n</table>\n</div>", | |
| "text/plain": " Date Revenue\n62 2006-01-31 1667\n63 2005-10-31 534\n64 2005-07-31 416\n65 2005-04-30 475\n66 2005-01-31 709" | |
| }, | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": "gme_revenue.tail(5)" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "## Question 5: Plot Tesla Stock Graph\n" | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "Use the `make_graph` function to graph the Tesla Stock Data, also provide a title for the graph. The structure to call the `make_graph` function is `make_graph(tesla_data, tesla_revenue, 'Tesla')`. Note the graph will only show data upto June 2021.\n" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 22, | |
| "metadata": { | |
| "scrolled": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment