Last active
July 24, 2024 10:03
-
-
Save lextoumbourou/d53db1a3de48ae1d629f78ee9d4fe5e8 to your computer and use it in GitHub Desktop.
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": [ | |
| "<p style=\"text-align:center\">\n", | |
| " <a href=\"https://skills.network/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0220ENSkillsNetwork900-2022-01-01\" target=\"_blank\">\n", | |
| " <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/assets/logos/SN_web_lightmode.png\" width=\"200\" alt=\"Skills Network Logo\">\n", | |
| " </a>\n", | |
| "</p>\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": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "***Note***:- If you are working Locally using anaconda, please uncomment the following code and execute it.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "#!pip install yfinance==0.2.38\n", | |
| "#!pip install pandas==2.2.2\n", | |
| "#!pip install nbformat" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Requirement already satisfied: yfinance in ./.env/lib/python3.9/site-packages (0.2.41)\n", | |
| "Requirement already satisfied: numpy>=1.16.5 in ./.env/lib/python3.9/site-packages (from yfinance) (2.0.1)\n", | |
| "Requirement already satisfied: pytz>=2022.5 in ./.env/lib/python3.9/site-packages (from yfinance) (2024.1)\n", | |
| "Requirement already satisfied: lxml>=4.9.1 in ./.env/lib/python3.9/site-packages (from yfinance) (5.2.2)\n", | |
| "Requirement already satisfied: requests>=2.31 in ./.env/lib/python3.9/site-packages (from yfinance) (2.32.3)\n", | |
| "Requirement already satisfied: frozendict>=2.3.4 in ./.env/lib/python3.9/site-packages (from yfinance) (2.4.4)\n", | |
| "Requirement already satisfied: beautifulsoup4>=4.11.1 in ./.env/lib/python3.9/site-packages (from yfinance) (4.12.3)\n", | |
| "Requirement already satisfied: pandas>=1.3.0 in ./.env/lib/python3.9/site-packages (from yfinance) (2.2.2)\n", | |
| "Requirement already satisfied: multitasking>=0.0.7 in ./.env/lib/python3.9/site-packages (from yfinance) (0.0.11)\n", | |
| "Requirement already satisfied: html5lib>=1.1 in ./.env/lib/python3.9/site-packages (from yfinance) (1.1)\n", | |
| "Requirement already satisfied: platformdirs>=2.0.0 in ./.env/lib/python3.9/site-packages (from yfinance) (4.2.2)\n", | |
| "Requirement already satisfied: peewee>=3.16.2 in ./.env/lib/python3.9/site-packages (from yfinance) (3.17.6)\n", | |
| "Requirement already satisfied: soupsieve>1.2 in ./.env/lib/python3.9/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.5)\n", | |
| "Requirement already satisfied: six>=1.9 in ./.env/lib/python3.9/site-packages (from html5lib>=1.1->yfinance) (1.16.0)\n", | |
| "Requirement already satisfied: webencodings in ./.env/lib/python3.9/site-packages (from html5lib>=1.1->yfinance) (0.5.1)\n", | |
| "Requirement already satisfied: python-dateutil>=2.8.2 in ./.env/lib/python3.9/site-packages (from pandas>=1.3.0->yfinance) (2.9.0.post0)\n", | |
| "Requirement already satisfied: tzdata>=2022.7 in ./.env/lib/python3.9/site-packages (from pandas>=1.3.0->yfinance) (2024.1)\n", | |
| "Requirement already satisfied: charset-normalizer<4,>=2 in ./.env/lib/python3.9/site-packages (from requests>=2.31->yfinance) (3.3.2)\n", | |
| "Requirement already satisfied: certifi>=2017.4.17 in ./.env/lib/python3.9/site-packages (from requests>=2.31->yfinance) (2024.7.4)\n", | |
| "Requirement already satisfied: urllib3<3,>=1.21.1 in ./.env/lib/python3.9/site-packages (from requests>=2.31->yfinance) (2.2.2)\n", | |
| "Requirement already satisfied: idna<4,>=2.5 in ./.env/lib/python3.9/site-packages (from requests>=2.31->yfinance) (3.7)\n", | |
| "\u001b[33mWARNING: You are using pip version 22.0.4; however, version 24.1.2 is available.\n", | |
| "You should consider upgrading via the '/Users/lex/code/uol/coursera/python-project-for-data-science/.env/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n", | |
| "\u001b[0mRequirement already satisfied: bs4 in ./.env/lib/python3.9/site-packages (0.0.2)\n", | |
| "Requirement already satisfied: beautifulsoup4 in ./.env/lib/python3.9/site-packages (from bs4) (4.12.3)\n", | |
| "Requirement already satisfied: soupsieve>1.2 in ./.env/lib/python3.9/site-packages (from beautifulsoup4->bs4) (2.5)\n", | |
| "\u001b[33mWARNING: You are using pip version 22.0.4; however, version 24.1.2 is available.\n", | |
| "You should consider upgrading via the '/Users/lex/code/uol/coursera/python-project-for-data-science/.env/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n", | |
| "\u001b[0mRequirement already satisfied: nbformat in ./.env/lib/python3.9/site-packages (5.10.4)\n", | |
| "Requirement already satisfied: traitlets>=5.1 in ./.env/lib/python3.9/site-packages (from nbformat) (5.14.3)\n", | |
| "Requirement already satisfied: jsonschema>=2.6 in ./.env/lib/python3.9/site-packages (from nbformat) (4.23.0)\n", | |
| "Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in ./.env/lib/python3.9/site-packages (from nbformat) (5.7.2)\n", | |
| "Requirement already satisfied: fastjsonschema>=2.15 in ./.env/lib/python3.9/site-packages (from nbformat) (2.20.0)\n", | |
| "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in ./.env/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat) (2023.12.1)\n", | |
| "Requirement already satisfied: attrs>=22.2.0 in ./.env/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat) (23.2.0)\n", | |
| "Requirement already satisfied: referencing>=0.28.4 in ./.env/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat) (0.35.1)\n", | |
| "Requirement already satisfied: rpds-py>=0.7.1 in ./.env/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat) (0.19.0)\n", | |
| "Requirement already satisfied: platformdirs>=2.5 in ./.env/lib/python3.9/site-packages (from jupyter-core!=5.0.*,>=4.12->nbformat) (4.2.2)\n", | |
| "\u001b[33mWARNING: You are using pip version 22.0.4; however, version 24.1.2 is available.\n", | |
| "You should consider upgrading via the '/Users/lex/code/uol/coursera/python-project-for-data-science/.env/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n", | |
| "\u001b[0m" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!pip install yfinance\n", | |
| "!pip install bs4\n", | |
| "!pip install nbformat" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import yfinance as yf\n", | |
| "import pandas as pd\n", | |
| "import requests\n", | |
| "from bs4 import BeautifulSoup\n", | |
| "import plotly.graph_objects as go\n", | |
| "from plotly.subplots import make_subplots" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "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.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import warnings\n", | |
| "# Ignore all warnings\n", | |
| "warnings.filterwarnings(\"ignore\", category=FutureWarning)" | |
| ] | |
| }, | |
| { | |
| "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": 5, | |
| "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), 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), 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": [ | |
| "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. \n", | |
| "> **Note: You don’t need to redefine the function for plotting graphs anywhere else in this notebook; just use the existing function.**\n" | |
| ] | |
| }, | |
| { | |
| "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": 9, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "tsla = 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": 10, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "tesla_data = tsla.history(period=\"max\")" | |
| ] | |
| }, | |
| { | |
| "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": 11, | |
| "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>2010-06-29 00:00:00-04:00</td>\n", | |
| " <td>1.266667</td>\n", | |
| " <td>1.666667</td>\n", | |
| " <td>1.169333</td>\n", | |
| " <td>1.592667</td>\n", | |
| " <td>281494500</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>2010-06-30 00:00:00-04:00</td>\n", | |
| " <td>1.719333</td>\n", | |
| " <td>2.028000</td>\n", | |
| " <td>1.553333</td>\n", | |
| " <td>1.588667</td>\n", | |
| " <td>257806500</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>2010-07-01 00:00:00-04:00</td>\n", | |
| " <td>1.666667</td>\n", | |
| " <td>1.728000</td>\n", | |
| " <td>1.351333</td>\n", | |
| " <td>1.464000</td>\n", | |
| " <td>123282000</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>2010-07-02 00:00:00-04:00</td>\n", | |
| " <td>1.533333</td>\n", | |
| " <td>1.540000</td>\n", | |
| " <td>1.247333</td>\n", | |
| " <td>1.280000</td>\n", | |
| " <td>77097000</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>2010-07-06 00:00:00-04:00</td>\n", | |
| " <td>1.333333</td>\n", | |
| " <td>1.333333</td>\n", | |
| " <td>1.055333</td>\n", | |
| " <td>1.074000</td>\n", | |
| " <td>103003500</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Date Open High Low Close \\\n", | |
| "0 2010-06-29 00:00:00-04:00 1.266667 1.666667 1.169333 1.592667 \n", | |
| "1 2010-06-30 00:00:00-04:00 1.719333 2.028000 1.553333 1.588667 \n", | |
| "2 2010-07-01 00:00:00-04:00 1.666667 1.728000 1.351333 1.464000 \n", | |
| "3 2010-07-02 00:00:00-04:00 1.533333 1.540000 1.247333 1.280000 \n", | |
| "4 2010-07-06 00:00:00-04:00 1.333333 1.333333 1.055333 1.074000 \n", | |
| "\n", | |
| " Volume Dividends Stock Splits \n", | |
| "0 281494500 0.0 0.0 \n", | |
| "1 257806500 0.0 0.0 \n", | |
| "2 123282000 0.0 0.0 \n", | |
| "3 77097000 0.0 0.0 \n", | |
| "4 103003500 0.0 0.0 " | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "tesla_data.reset_index(inplace=True)\n", | |
| "tesla_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://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm Save the text of the response as a variable named `html_data`.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "html_data = requests.get(\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm\").text" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Parse the html data using `beautiful_soup` using parser i.e `html5lib` or `html.parser`. Make sure to use the `html_data` with the content parameter as follow `html_data.content` .\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "soup = BeautifulSoup(html_data)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "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`.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "<details><summary>Step-by-step instructions</summary>\n", | |
| "\n", | |
| "```\n", | |
| "\n", | |
| "Here are the step-by-step instructions:\n", | |
| "\n", | |
| "1. Find All Tables: Start by searching for all HTML tables on a webpage using `soup.find_all('table')`.\n", | |
| "2. Identify the Relevant Table: then loops through each table. If a table contains the text “Tesla Quarterly Revenue,”, select that table.\n", | |
| "3. Initialize a DataFrame: Create an empty Pandas DataFrame called `tesla_revenue` with columns “Date” and “Revenue.”\n", | |
| "4. Loop Through Rows: For each row in the relevant table, extract the data from the first and second columns (date and revenue).\n", | |
| "5. Clean Revenue Data: Remove dollar signs and commas from the revenue value.\n", | |
| "6. Add Rows to DataFrame: Create a new row in the DataFrame with the extracted date and cleaned revenue values.\n", | |
| "7. Repeat for All Rows: Continue this process for all rows in the table.\n", | |
| "\n", | |
| "```\n", | |
| "</details>\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "<details><summary>Click here if you need help locating the table</summary>\n", | |
| "\n", | |
| "```\n", | |
| " \n", | |
| "Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab\n", | |
| " \n", | |
| "soup.find_all(\"tbody\")[1]\n", | |
| " \n", | |
| "If you want to use the read_html function the table is located at index 1\n", | |
| "\n", | |
| "We are focusing on quarterly revenue in the lab.\n", | |
| "```\n", | |
| "\n", | |
| "</details>\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 35, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "tesla_revenue = pd.DataFrame(columns=[\"Date\", \"Revenue\"])\n", | |
| "for tr in soup.find_all(\"tbody\")[1].find_all(\"tr\"):\n", | |
| " col =tr.find_all(\"td\")\n", | |
| " date = col[0].text\n", | |
| " revenue = col[1].text\n", | |
| " \n", | |
| " # Finally we append the data of each row to the table\n", | |
| " tesla_revenue = pd.concat([tesla_revenue,pd.DataFrame({\"Date\":[date], \"Revenue\":[revenue]})], ignore_index=True) " | |
| ] | |
| }, | |
| { | |
| "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": 37, | |
| "metadata": {}, | |
| "outputs": [], | |
| "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": 38, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "tesla_revenue.dropna(inplace=True)\n", | |
| "\n", | |
| "tesla_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": 39, | |
| "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>48</th>\n", | |
| " <td>2010-09-30</td>\n", | |
| " <td>$31</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>49</th>\n", | |
| " <td>2010-06-30</td>\n", | |
| " <td>$28</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>50</th>\n", | |
| " <td>2010-03-31</td>\n", | |
| " <td>$21</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>52</th>\n", | |
| " <td>2009-09-30</td>\n", | |
| " <td>$46</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>53</th>\n", | |
| " <td>2009-06-30</td>\n", | |
| " <td>$27</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Date Revenue\n", | |
| "48 2010-09-30 $31\n", | |
| "49 2010-06-30 $28\n", | |
| "50 2010-03-31 $21\n", | |
| "52 2009-09-30 $46\n", | |
| "53 2009-06-30 $27" | |
| ] | |
| }, | |
| "execution_count": 39, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "tesla_revenue.tail()" | |
| ] | |
| }, | |
| { | |
| "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": 41, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "gme = 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": 42, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "gme_data = gme.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": 43, | |
| "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 00:00:00-05:00</td>\n", | |
| " <td>1.620129</td>\n", | |
| " <td>1.693350</td>\n", | |
| " <td>1.603296</td>\n", | |
| " <td>1.691667</td>\n", | |
| " <td>76216000</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>2002-02-14 00:00:00-05:00</td>\n", | |
| " <td>1.712708</td>\n", | |
| " <td>1.716074</td>\n", | |
| " <td>1.670626</td>\n", | |
| " <td>1.683251</td>\n", | |
| " <td>11021600</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>2002-02-15 00:00:00-05:00</td>\n", | |
| " <td>1.683251</td>\n", | |
| " <td>1.687459</td>\n", | |
| " <td>1.658002</td>\n", | |
| " <td>1.674834</td>\n", | |
| " <td>8389600</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>2002-02-19 00:00:00-05:00</td>\n", | |
| " <td>1.666417</td>\n", | |
| " <td>1.666417</td>\n", | |
| " <td>1.578047</td>\n", | |
| " <td>1.607504</td>\n", | |
| " <td>7410400</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>2002-02-20 00:00:00-05:00</td>\n", | |
| " <td>1.615920</td>\n", | |
| " <td>1.662209</td>\n", | |
| " <td>1.603296</td>\n", | |
| " <td>1.662209</td>\n", | |
| " <td>6892800</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Date Open High Low Close Volume \\\n", | |
| "0 2002-02-13 00:00:00-05:00 1.620129 1.693350 1.603296 1.691667 76216000 \n", | |
| "1 2002-02-14 00:00:00-05:00 1.712708 1.716074 1.670626 1.683251 11021600 \n", | |
| "2 2002-02-15 00:00:00-05:00 1.683251 1.687459 1.658002 1.674834 8389600 \n", | |
| "3 2002-02-19 00:00:00-05:00 1.666417 1.666417 1.578047 1.607504 7410400 \n", | |
| "4 2002-02-20 00:00:00-05:00 1.615920 1.662209 1.603296 1.662209 6892800 \n", | |
| "\n", | |
| " Dividends Stock Splits \n", | |
| "0 0.0 0.0 \n", | |
| "1 0.0 0.0 \n", | |
| "2 0.0 0.0 \n", | |
| "3 0.0 0.0 \n", | |
| "4 0.0 0.0 " | |
| ] | |
| }, | |
| "execution_count": 43, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "gme_data.reset_index(inplace=True)\n", | |
| "gme_data.head()" | |
| ] | |
| }, | |
| { | |
| "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://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html. Save the text of the response as a variable named `html_data_2`.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 44, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "html_data_2 = requests.get(\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html\").text" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Parse the html data using `beautiful_soup` using parser i.e `html5lib` or `html.parser`.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 45, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "soup2 = BeautifulSoup(html_data_2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "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.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "> **Note: Use the 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", | |
| " \n", | |
| "Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab\n", | |
| " \n", | |
| "soup.find_all(\"tbody\")[1]\n", | |
| " \n", | |
| "If 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": 47, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "gme_revenue = pd.DataFrame(columns=[\"Date\", \"Revenue\"])\n", | |
| "\n", | |
| "for tr in soup2.find_all(\"tbody\")[1].find_all(\"tr\"):\n", | |
| " col =tr.find_all(\"td\")\n", | |
| " date = col[0].text\n", | |
| " revenue = col[1].text\n", | |
| " \n", | |
| " # Finally we append the data of each row to the table\n", | |
| " gme_revenue = pd.concat([gme_revenue,pd.DataFrame({\"Date\":[date], \"Revenue\":[revenue]})], 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": 48, | |
| "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>57</th>\n", | |
| " <td>2006-01-31</td>\n", | |
| " <td>$1,667</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>58</th>\n", | |
| " <td>2005-10-31</td>\n", | |
| " <td>$534</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>59</th>\n", | |
| " <td>2005-07-31</td>\n", | |
| " <td>$416</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>60</th>\n", | |
| " <td>2005-04-30</td>\n", | |
| " <td>$475</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>61</th>\n", | |
| " <td>2005-01-31</td>\n", | |
| " <td>$709</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Date Revenue\n", | |
| "57 2006-01-31 $1,667\n", | |
| "58 2005-10-31 $534\n", | |
| "59 2005-07-31 $416\n", | |
| "60 2005-04-30 $475\n", | |
| "61 2005-01-31 $709" | |
| ] | |
| }, | |
| "execution_count": 48, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "gme_revenue.tail()" | |
| ] | |
| }, | |
| { | |
| "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. Note the graph will only show data upto June 2021.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "<details><summary>Hint</summary>\n", | |
| "\n", | |
| "```\n", | |
| "\n", | |
| "You just need to invoke the make_graph function with the required parameter to print the graphs.The structure to call the `make_graph` function is `make_graph(tesla_data, tesla_revenue, 'Tesla')`.\n", | |
| "\n", | |
| "```\n", | |
| " \n", | |
| "</details>\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 55, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "tesla_revenue.Revenue = tesla_revenue.Revenue.str.replace(\"$\", \"\").str.replace(\",\", \"\").astype(float)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 56, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| " <script type=\"text/javascript\">\n", | |
| " window.PlotlyConfig = {MathJaxConfig: 'local'};\n", | |
| " if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n", | |
| " if (typeof require !== 'undefined') {\n", | |
| " require.undef(\"plotly\");\n", | |
| " define('plotly', function(require, exports, module) {\n", | |
| " /**\n", | |
| "* plotly.js v2.34.0\n", | |
| "* Copyright 2012-2024, Plotly, Inc.\n", | |
| "* All rights reserved.\n", | |
| "* Licensed under the MIT license\n", | |
| "*/\n", | |
| "/*! For license information please see plotly.min.js.LICENSE.txt */\n", |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment