From 03ec1d122e98c7e6fd6cc15bb07f34d7dfeee510 Mon Sep 17 00:00:00 2001 From: Andy Kuemmel <kuemmel@wisc.edu> Date: Wed, 16 Nov 2022 11:41:43 -0600 Subject: [PATCH] Upload New File --- .../lec29_Nov16_Web1/lec29_completed.ipynb | 1916 +++++++++++++++++ 1 file changed, 1916 insertions(+) create mode 100644 f22/andy_lec_notes/lec29_Nov16_Web1/lec29_completed.ipynb diff --git a/f22/andy_lec_notes/lec29_Nov16_Web1/lec29_completed.ipynb b/f22/andy_lec_notes/lec29_Nov16_Web1/lec29_completed.ipynb new file mode 100644 index 0000000..8bf9a5b --- /dev/null +++ b/f22/andy_lec_notes/lec29_Nov16_Web1/lec29_completed.ipynb @@ -0,0 +1,1916 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import requests # We will learn about this today!\n", + "import json\n", + "import pandas as pd\n", + "from pandas import Series, DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "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>Title</th>\n", + " <th>Genre</th>\n", + " <th>Director</th>\n", + " <th>Cast</th>\n", + " <th>Year</th>\n", + " <th>Runtime</th>\n", + " <th>Rating</th>\n", + " <th>Revenue</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>1063</th>\n", + " <td>Guardians of the Galaxy Vol. 2</td>\n", + " <td>Action, Adventure, Comedy</td>\n", + " <td>James Gunn</td>\n", + " <td>Chris Pratt, Zoe Saldana, Dave Bautista, Vin D...</td>\n", + " <td>2017</td>\n", + " <td>136</td>\n", + " <td>7.6</td>\n", + " <td>389.81</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1064</th>\n", + " <td>Baby Driver</td>\n", + " <td>Action, Crime, Drama</td>\n", + " <td>Edgar Wright</td>\n", + " <td>Ansel Elgort, Jon Bernthal, Jon Hamm, Eiza Gon...</td>\n", + " <td>2017</td>\n", + " <td>113</td>\n", + " <td>7.6</td>\n", + " <td>107.83</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1065</th>\n", + " <td>Only the Brave</td>\n", + " <td>Action, Biography, Drama</td>\n", + " <td>Joseph Kosinski</td>\n", + " <td>Josh Brolin, Miles Teller, Jeff Bridges, Jenni...</td>\n", + " <td>2017</td>\n", + " <td>134</td>\n", + " <td>7.6</td>\n", + " <td>18.34</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1066</th>\n", + " <td>Incredibles 2</td>\n", + " <td>Animation, Action, Adventure</td>\n", + " <td>Brad Bird</td>\n", + " <td>Craig T. Nelson, Holly Hunter, Sarah Vowell, H...</td>\n", + " <td>2018</td>\n", + " <td>118</td>\n", + " <td>7.6</td>\n", + " <td>608.58</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1067</th>\n", + " <td>A Star Is Born</td>\n", + " <td>Drama, Music, Romance</td>\n", + " <td>Bradley Cooper</td>\n", + " <td>Lady Gaga, Bradley Cooper, Sam Elliott, Greg G...</td>\n", + " <td>2018</td>\n", + " <td>136</td>\n", + " <td>7.6</td>\n", + " <td>215.29</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " Title Genre \\\n", + "1063 Guardians of the Galaxy Vol. 2 Action, Adventure, Comedy \n", + "1064 Baby Driver Action, Crime, Drama \n", + "1065 Only the Brave Action, Biography, Drama \n", + "1066 Incredibles 2 Animation, Action, Adventure \n", + "1067 A Star Is Born Drama, Music, Romance \n", + "\n", + " Director Cast \\\n", + "1063 James Gunn Chris Pratt, Zoe Saldana, Dave Bautista, Vin D... \n", + "1064 Edgar Wright Ansel Elgort, Jon Bernthal, Jon Hamm, Eiza Gon... \n", + "1065 Joseph Kosinski Josh Brolin, Miles Teller, Jeff Bridges, Jenni... \n", + "1066 Brad Bird Craig T. Nelson, Holly Hunter, Sarah Vowell, H... \n", + "1067 Bradley Cooper Lady Gaga, Bradley Cooper, Sam Elliott, Greg G... \n", + "\n", + " Year Runtime Rating Revenue \n", + "1063 2017 136 7.6 389.81 \n", + "1064 2017 113 7.6 107.83 \n", + "1065 2017 134 7.6 18.34 \n", + "1066 2018 118 7.6 608.58 \n", + "1067 2018 136 7.6 215.29 " + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Warmup 1: Read the data from \"new_movie_data.csv\" into a pandas DataFrame called \"movies\"\n", + "movies = None\n", + "movies = pd.read_csv(\"new_movie_data.csv\")\n", + "movies.tail()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2006 to 2020\n" + ] + } + ], + "source": [ + "# Warmup 2: What years does this new movie dataset cover? (min and max?)\n", + "year_min = movies['Year'].min()\n", + "year_max = movies['Year'].max()\n", + "print(year_min, \"to\", year_max)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15300000.0\n", + "3450000.0\n", + "20600000.0\n" + ] + } + ], + "source": [ + "# Warmup 3a: What does this function do?\n", + "def format_revenue(revenue):\n", + " if type(revenue) == float: # need this in here if we run code multiple times\n", + " return revenue\n", + " elif revenue[-1] == 'M': # some have an \"M\" at the end\n", + " return float(revenue[:-1]) * 1e6\n", + " else: # otherwise, assume millions.\n", + " return float(revenue) * 1e6\n", + " \n", + "# always good to test a function like this right awaty\n", + "print(format_revenue(\"15.3M\"))\n", + "print(format_revenue(3450000.0))\n", + "print(format_revenue(\"20.6\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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>Title</th>\n", + " <th>Genre</th>\n", + " <th>Director</th>\n", + " <th>Cast</th>\n", + " <th>Year</th>\n", + " <th>Runtime</th>\n", + " <th>Rating</th>\n", + " <th>Revenue</th>\n", + " <th>CountableRevenue</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>0</th>\n", + " <td>Guardians of the Galaxy</td>\n", + " <td>Action,Adventure,Sci-Fi</td>\n", + " <td>James Gunn</td>\n", + " <td>Chris Pratt, Vin Diesel, Bradley Cooper, Zoe S...</td>\n", + " <td>2014</td>\n", + " <td>121</td>\n", + " <td>8.1</td>\n", + " <td>333.13</td>\n", + " <td>333130000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1</th>\n", + " <td>Prometheus</td>\n", + " <td>Adventure,Mystery,Sci-Fi</td>\n", + " <td>Ridley Scott</td>\n", + " <td>Noomi Rapace, Logan Marshall-Green, Michael ...</td>\n", + " <td>2012</td>\n", + " <td>124</td>\n", + " <td>7.0</td>\n", + " <td>126.46M</td>\n", + " <td>126460000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>2</th>\n", + " <td>Split</td>\n", + " <td>Horror,Thriller</td>\n", + " <td>M. Night Shyamalan</td>\n", + " <td>James McAvoy, Anya Taylor-Joy, Haley Lu Richar...</td>\n", + " <td>2016</td>\n", + " <td>117</td>\n", + " <td>7.3</td>\n", + " <td>138.12M</td>\n", + " <td>138120000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>3</th>\n", + " <td>Sing</td>\n", + " <td>Animation,Comedy,Family</td>\n", + " <td>Christophe Lourdelet</td>\n", + " <td>Matthew McConaughey,Reese Witherspoon, Seth Ma...</td>\n", + " <td>2016</td>\n", + " <td>108</td>\n", + " <td>7.2</td>\n", + " <td>270.32</td>\n", + " <td>270320000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>4</th>\n", + " <td>Suicide Squad</td>\n", + " <td>Action,Adventure,Fantasy</td>\n", + " <td>David Ayer</td>\n", + " <td>Will Smith, Jared Leto, Margot Robbie, Viola D...</td>\n", + " <td>2016</td>\n", + " <td>123</td>\n", + " <td>6.2</td>\n", + " <td>325.02</td>\n", + " <td>325020000.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", + " <td>...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1063</th>\n", + " <td>Guardians of the Galaxy Vol. 2</td>\n", + " <td>Action, Adventure, Comedy</td>\n", + " <td>James Gunn</td>\n", + " <td>Chris Pratt, Zoe Saldana, Dave Bautista, Vin D...</td>\n", + " <td>2017</td>\n", + " <td>136</td>\n", + " <td>7.6</td>\n", + " <td>389.81</td>\n", + " <td>389810000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1064</th>\n", + " <td>Baby Driver</td>\n", + " <td>Action, Crime, Drama</td>\n", + " <td>Edgar Wright</td>\n", + " <td>Ansel Elgort, Jon Bernthal, Jon Hamm, Eiza Gon...</td>\n", + " <td>2017</td>\n", + " <td>113</td>\n", + " <td>7.6</td>\n", + " <td>107.83</td>\n", + " <td>107830000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1065</th>\n", + " <td>Only the Brave</td>\n", + " <td>Action, Biography, Drama</td>\n", + " <td>Joseph Kosinski</td>\n", + " <td>Josh Brolin, Miles Teller, Jeff Bridges, Jenni...</td>\n", + " <td>2017</td>\n", + " <td>134</td>\n", + " <td>7.6</td>\n", + " <td>18.34</td>\n", + " <td>18340000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1066</th>\n", + " <td>Incredibles 2</td>\n", + " <td>Animation, Action, Adventure</td>\n", + " <td>Brad Bird</td>\n", + " <td>Craig T. Nelson, Holly Hunter, Sarah Vowell, H...</td>\n", + " <td>2018</td>\n", + " <td>118</td>\n", + " <td>7.6</td>\n", + " <td>608.58</td>\n", + " <td>608580000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1067</th>\n", + " <td>A Star Is Born</td>\n", + " <td>Drama, Music, Romance</td>\n", + " <td>Bradley Cooper</td>\n", + " <td>Lady Gaga, Bradley Cooper, Sam Elliott, Greg G...</td>\n", + " <td>2018</td>\n", + " <td>136</td>\n", + " <td>7.6</td>\n", + " <td>215.29</td>\n", + " <td>215290000.0</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "<p>1068 rows × 9 columns</p>\n", + "</div>" + ], + "text/plain": [ + " Title Genre \\\n", + "0 Guardians of the Galaxy Action,Adventure,Sci-Fi \n", + "1 Prometheus Adventure,Mystery,Sci-Fi \n", + "2 Split Horror,Thriller \n", + "3 Sing Animation,Comedy,Family \n", + "4 Suicide Squad Action,Adventure,Fantasy \n", + "... ... ... \n", + "1063 Guardians of the Galaxy Vol. 2 Action, Adventure, Comedy \n", + "1064 Baby Driver Action, Crime, Drama \n", + "1065 Only the Brave Action, Biography, Drama \n", + "1066 Incredibles 2 Animation, Action, Adventure \n", + "1067 A Star Is Born Drama, Music, Romance \n", + "\n", + " Director Cast \\\n", + "0 James Gunn Chris Pratt, Vin Diesel, Bradley Cooper, Zoe S... \n", + "1 Ridley Scott Noomi Rapace, Logan Marshall-Green, Michael ... \n", + "2 M. Night Shyamalan James McAvoy, Anya Taylor-Joy, Haley Lu Richar... \n", + "3 Christophe Lourdelet Matthew McConaughey,Reese Witherspoon, Seth Ma... \n", + "4 David Ayer Will Smith, Jared Leto, Margot Robbie, Viola D... \n", + "... ... ... \n", + "1063 James Gunn Chris Pratt, Zoe Saldana, Dave Bautista, Vin D... \n", + "1064 Edgar Wright Ansel Elgort, Jon Bernthal, Jon Hamm, Eiza Gon... \n", + "1065 Joseph Kosinski Josh Brolin, Miles Teller, Jeff Bridges, Jenni... \n", + "1066 Brad Bird Craig T. Nelson, Holly Hunter, Sarah Vowell, H... \n", + "1067 Bradley Cooper Lady Gaga, Bradley Cooper, Sam Elliott, Greg G... \n", + "\n", + " Year Runtime Rating Revenue CountableRevenue \n", + "0 2014 121 8.1 333.13 333130000.0 \n", + "1 2012 124 7.0 126.46M 126460000.0 \n", + "2 2016 117 7.3 138.12M 138120000.0 \n", + "3 2016 108 7.2 270.32 270320000.0 \n", + "4 2016 123 6.2 325.02 325020000.0 \n", + "... ... ... ... ... ... \n", + "1063 2017 136 7.6 389.81 389810000.0 \n", + "1064 2017 113 7.6 107.83 107830000.0 \n", + "1065 2017 134 7.6 18.34 18340000.0 \n", + "1066 2018 118 7.6 608.58 608580000.0 \n", + "1067 2018 136 7.6 215.29 215290000.0 \n", + "\n", + "[1068 rows x 9 columns]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Warmup 3b: Using the above function, create a new column called\n", + "# \"CountableRevenue\" with the revenue as a float.\n", + "\n", + "movies[\"CountableRevenue\"] = movies['Revenue'].apply(format_revenue)\n", + "movies" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "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>Title</th>\n", + " <th>Genre</th>\n", + " <th>Director</th>\n", + " <th>Cast</th>\n", + " <th>Year</th>\n", + " <th>Runtime</th>\n", + " <th>Rating</th>\n", + " <th>Revenue</th>\n", + " <th>CountableRevenue</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>0</th>\n", + " <td>Guardians of the Galaxy</td>\n", + " <td>Action,Adventure,Sci-Fi</td>\n", + " <td>James Gunn</td>\n", + " <td>Chris Pratt, Vin Diesel, Bradley Cooper, Zoe S...</td>\n", + " <td>2014</td>\n", + " <td>121</td>\n", + " <td>8.1</td>\n", + " <td>333.13</td>\n", + " <td>333130000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1</th>\n", + " <td>Prometheus</td>\n", + " <td>Adventure,Mystery,Sci-Fi</td>\n", + " <td>Ridley Scott</td>\n", + " <td>Noomi Rapace, Logan Marshall-Green, Michael ...</td>\n", + " <td>2012</td>\n", + " <td>124</td>\n", + " <td>7.0</td>\n", + " <td>126.46M</td>\n", + " <td>126460000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>2</th>\n", + " <td>Split</td>\n", + " <td>Horror,Thriller</td>\n", + " <td>M. Night Shyamalan</td>\n", + " <td>James McAvoy, Anya Taylor-Joy, Haley Lu Richar...</td>\n", + " <td>2016</td>\n", + " <td>117</td>\n", + " <td>7.3</td>\n", + " <td>138.12M</td>\n", + " <td>138120000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>3</th>\n", + " <td>Sing</td>\n", + " <td>Animation,Comedy,Family</td>\n", + " <td>Christophe Lourdelet</td>\n", + " <td>Matthew McConaughey,Reese Witherspoon, Seth Ma...</td>\n", + " <td>2016</td>\n", + " <td>108</td>\n", + " <td>7.2</td>\n", + " <td>270.32</td>\n", + " <td>270320000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>4</th>\n", + " <td>Suicide Squad</td>\n", + " <td>Action,Adventure,Fantasy</td>\n", + " <td>David Ayer</td>\n", + " <td>Will Smith, Jared Leto, Margot Robbie, Viola D...</td>\n", + " <td>2016</td>\n", + " <td>123</td>\n", + " <td>6.2</td>\n", + " <td>325.02</td>\n", + " <td>325020000.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", + " <td>...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1063</th>\n", + " <td>Guardians of the Galaxy Vol. 2</td>\n", + " <td>Action, Adventure, Comedy</td>\n", + " <td>James Gunn</td>\n", + " <td>Chris Pratt, Zoe Saldana, Dave Bautista, Vin D...</td>\n", + " <td>2017</td>\n", + " <td>136</td>\n", + " <td>7.6</td>\n", + " <td>389.81</td>\n", + " <td>389810000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1064</th>\n", + " <td>Baby Driver</td>\n", + " <td>Action, Crime, Drama</td>\n", + " <td>Edgar Wright</td>\n", + " <td>Ansel Elgort, Jon Bernthal, Jon Hamm, Eiza Gon...</td>\n", + " <td>2017</td>\n", + " <td>113</td>\n", + " <td>7.6</td>\n", + " <td>107.83</td>\n", + " <td>107830000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1065</th>\n", + " <td>Only the Brave</td>\n", + " <td>Action, Biography, Drama</td>\n", + " <td>Joseph Kosinski</td>\n", + " <td>Josh Brolin, Miles Teller, Jeff Bridges, Jenni...</td>\n", + " <td>2017</td>\n", + " <td>134</td>\n", + " <td>7.6</td>\n", + " <td>18.34</td>\n", + " <td>18340000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1066</th>\n", + " <td>Incredibles 2</td>\n", + " <td>Animation, Action, Adventure</td>\n", + " <td>Brad Bird</td>\n", + " <td>Craig T. Nelson, Holly Hunter, Sarah Vowell, H...</td>\n", + " <td>2018</td>\n", + " <td>118</td>\n", + " <td>7.6</td>\n", + " <td>608.58</td>\n", + " <td>608580000.0</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1067</th>\n", + " <td>A Star Is Born</td>\n", + " <td>Drama, Music, Romance</td>\n", + " <td>Bradley Cooper</td>\n", + " <td>Lady Gaga, Bradley Cooper, Sam Elliott, Greg G...</td>\n", + " <td>2018</td>\n", + " <td>136</td>\n", + " <td>7.6</td>\n", + " <td>215.29</td>\n", + " <td>215290000.0</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "<p>1068 rows × 9 columns</p>\n", + "</div>" + ], + "text/plain": [ + " Title Genre \\\n", + "0 Guardians of the Galaxy Action,Adventure,Sci-Fi \n", + "1 Prometheus Adventure,Mystery,Sci-Fi \n", + "2 Split Horror,Thriller \n", + "3 Sing Animation,Comedy,Family \n", + "4 Suicide Squad Action,Adventure,Fantasy \n", + "... ... ... \n", + "1063 Guardians of the Galaxy Vol. 2 Action, Adventure, Comedy \n", + "1064 Baby Driver Action, Crime, Drama \n", + "1065 Only the Brave Action, Biography, Drama \n", + "1066 Incredibles 2 Animation, Action, Adventure \n", + "1067 A Star Is Born Drama, Music, Romance \n", + "\n", + " Director Cast \\\n", + "0 James Gunn Chris Pratt, Vin Diesel, Bradley Cooper, Zoe S... \n", + "1 Ridley Scott Noomi Rapace, Logan Marshall-Green, Michael ... \n", + "2 M. Night Shyamalan James McAvoy, Anya Taylor-Joy, Haley Lu Richar... \n", + "3 Christophe Lourdelet Matthew McConaughey,Reese Witherspoon, Seth Ma... \n", + "4 David Ayer Will Smith, Jared Leto, Margot Robbie, Viola D... \n", + "... ... ... \n", + "1063 James Gunn Chris Pratt, Zoe Saldana, Dave Bautista, Vin D... \n", + "1064 Edgar Wright Ansel Elgort, Jon Bernthal, Jon Hamm, Eiza Gon... \n", + "1065 Joseph Kosinski Josh Brolin, Miles Teller, Jeff Bridges, Jenni... \n", + "1066 Brad Bird Craig T. Nelson, Holly Hunter, Sarah Vowell, H... \n", + "1067 Bradley Cooper Lady Gaga, Bradley Cooper, Sam Elliott, Greg G... \n", + "\n", + " Year Runtime Rating Revenue CountableRevenue \n", + "0 2014 121 8.1 333.13 333130000.0 \n", + "1 2012 124 7.0 126.46M 126460000.0 \n", + "2 2016 117 7.3 138.12M 138120000.0 \n", + "3 2016 108 7.2 270.32 270320000.0 \n", + "4 2016 123 6.2 325.02 325020000.0 \n", + "... ... ... ... ... ... \n", + "1063 2017 136 7.6 389.81 389810000.0 \n", + "1064 2017 113 7.6 107.83 107830000.0 \n", + "1065 2017 134 7.6 18.34 18340000.0 \n", + "1066 2018 118 7.6 608.58 608580000.0 \n", + "1067 2018 136 7.6 215.29 215290000.0 \n", + "\n", + "[1068 rows x 9 columns]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Warmup 4: What are the top 10 highest-revenue movies?\n", + "\n", + "movies.sort_values(by='CountableRevenue', ascending=False).head(10)\n", + "movies" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pandas extra tutorial...not required\n", + "https://www.w3schools.com/python/pandas/default.asp\n", + "\n", + "### Pandas cheat sheet\n", + "https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lecture 29: Web 1 - Getting Data\n", + "Learning Objectives\n", + "\n", + "- Make a request for data using `requests.get(URL)`\n", + "- Check the status of a request/response\n", + "- Extract the text of a response\n", + "- Create a json file from a response\n", + "- State and practice good etiquette when getting data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Make a request for data using `requests.get(URL)`\n", + "- URL: https://www.msyamkumar.com/hello.txt" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'requests.models.Response'>\n", + "200\n", + "Hello CS220 / CS319 students! Welcome to my website. Hope you are staying safe and healthy!\n", + "\n" + ] + } + ], + "source": [ + "url = \"https://www.msyamkumar.com/hello.txt\"\n", + "r = requests.get(url) # r is the response\n", + "print(type(r))\n", + "print(r.status_code)\n", + "print(r.text)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### HTTP Status Codes you need to know\n", + "\n", + "- 200: success\n", + "- 404: not found\n", + "\n", + "Here is a list of all status codes, you do NOT need to memorize it\n", + "\n", + "https://en.wikipedia.org/wiki/List_of_HTTP_status_codes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Additional (not required) info about Request objects\n", + "https://www.w3schools.com/python/module_requests.asp" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "404\n", + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", + "<Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message><Key>meena/hello.txttttt</Key><RequestId>Q4KZ42AMN9J51Q4V</RequestId><HostId>t4N4b4xpqy83XlliVFyYd1puYPrZPj6MNS4oiiP13RKA4bNUUv4eaq15Dv+FVwYCQf3QGoUltCo=</HostId></Error>\n" + ] + } + ], + "source": [ + "# Q: What if the web site does not exist?\n", + "typo_url = \"https://www.msyamkumar.com/hello.txttttt\"\n", + "r = requests.get(typo_url)\n", + "print(r.status_code)\n", + "print(r.text)\n", + "\n", + "# A: We get a 404 (client error)\n", + "# A: we get text, but the text is not from the client\n", + "# so we have to make sure we check the status of our request" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Check the status of a request/response\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "404\n" + ] + }, + { + "ename": "AssertionError", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [11]\u001b[0m, in \u001b[0;36m<cell line: 5>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m r \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mget(typo_url)\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(r\u001b[38;5;241m.\u001b[39mstatus_code)\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m r\u001b[38;5;241m.\u001b[39mstatus_code \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m220\u001b[39m \u001b[38;5;66;03m# change this\u001b[39;00m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(r\u001b[38;5;241m.\u001b[39mtext)\n", + "\u001b[0;31mAssertionError\u001b[0m: " + ] + } + ], + "source": [ + "# We can check for a status_code error by using an assert\n", + "typo_url = \"https://www.msyamkumar.com/hello.txttttt\"\n", + "r = requests.get(typo_url)\n", + "print(r.status_code)\n", + "assert r.status_code == 220 # change this\n", + "\n", + "print(r.text)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "ename": "HTTPError", + "evalue": "404 Client Error: Not Found for url: https://www.msyamkumar.com/hello.txttttt", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mHTTPError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [12]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Instead of using an assert, we often use raise_for_status()\u001b[39;00m\n\u001b[1;32m 2\u001b[0m r \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mget(typo_url)\n\u001b[0;32m----> 3\u001b[0m \u001b[43mr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m#similar to assert r.status_code == 200\u001b[39;00m\n\u001b[1;32m 4\u001b[0m r\u001b[38;5;241m.\u001b[39mtext\n", + "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/requests/models.py:960\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 957\u001b[0m http_error_msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mu\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m Server Error: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m for url: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m%\u001b[39m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstatus_code, reason, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39murl)\n\u001b[1;32m 959\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m http_error_msg:\n\u001b[0;32m--> 960\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(http_error_msg, response\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n", + "\u001b[0;31mHTTPError\u001b[0m: 404 Client Error: Not Found for url: https://www.msyamkumar.com/hello.txttttt" + ] + } + ], + "source": [ + "# Instead of using an assert, we often use raise_for_status()\n", + "r = requests.get(typo_url)\n", + "r.raise_for_status() #similar to assert r.status_code == 200\n", + "r.text\n", + "\n", + "# Note the error you get.... We will use this in the next cell" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'HTTPError' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mHTTPError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [14]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m r \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mget(typo_url)\n\u001b[0;32m----> 5\u001b[0m \u001b[43mr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m#similar to assert r.status_code == 200\u001b[39;00m\n\u001b[1;32m 6\u001b[0m r\u001b[38;5;241m.\u001b[39mtext\n", + "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/requests/models.py:960\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 959\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m http_error_msg:\n\u001b[0;32m--> 960\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(http_error_msg, response\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n", + "\u001b[0;31mHTTPError\u001b[0m: 404 Client Error: Not Found for url: https://www.msyamkumar.com/hello.txttttt", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [14]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m r\u001b[38;5;241m.\u001b[39mraise_for_status() \u001b[38;5;66;03m#similar to assert r.status_code == 200\u001b[39;00m\n\u001b[1;32m 6\u001b[0m r\u001b[38;5;241m.\u001b[39mtext\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[43mHTTPError\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moops!!\u001b[39m\u001b[38;5;124m\"\u001b[39m, e)\n", + "\u001b[0;31mNameError\u001b[0m: name 'HTTPError' is not defined" + ] + } + ], + "source": [ + "# Let's try to catch that error\n", + "\n", + "try:\n", + " r = requests.get(typo_url)\n", + " r.raise_for_status() #similar to assert r.status_code == 200\n", + " r.text\n", + "except HTTPError as e:\n", + " print(\"oops!!\", e)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "oops!! 404 Client Error: Not Found for url: https://www.msyamkumar.com/hello.txttttt\n" + ] + } + ], + "source": [ + "# we often need to prepend the names of exceptions with the name of the module\n", + "# fix the error from above\n", + "\n", + "try:\n", + " r = requests.get(typo_url)\n", + " r.raise_for_status() #similar to assert r.status_code == 200\n", + " r.text\n", + "except requests.HTTPError as e:\n", + " print(\"oops!!\", e)\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create a json file from a response\n", + "\n", + "- URL: https://www.msyamkumar.com/scores.json\n", + "- `json.load` (FILE_OBJECT)\n", + "- `json.loads` (STRING)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"alice\": 100,\n", + " \"bob\": 200,\n", + " \"cindy\": 300\n", + "}\n", + "\n", + "<class 'dict'> {'alice': 100, 'bob': 200, 'cindy': 300}\n" + ] + } + ], + "source": [ + "# GETting a JSON file, the long way\n", + "url = \"https://www.msyamkumar.com/scores.json\"\n", + "r = requests.get(url)\n", + "r.raise_for_status()\n", + "urltext = r.text\n", + "print(urltext)\n", + "d = json.loads(urltext)\n", + "print(type(d), d)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'dict'> {'alice': 100, 'bob': 200, 'cindy': 300}\n" + ] + } + ], + "source": [ + "# GETting a JSON file, the shortcut way\n", + "url = \"https://www.msyamkumar.com/scores.json\"\n", + "#Shortcut to bypass using json.loads()\n", + "r = requests.get(url)\n", + "r.raise_for_status()\n", + "d2 = r.json()\n", + "print(type(d2), d2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Good GET Etiquette\n", + "\n", + "Don't make a lot of requests to the same server all at once.\n", + " - Requests use up the server's time\n", + " - Major websites will often ban users who make too many requests\n", + " - You can break a server....similar to DDoS attacks (DON'T DO THIS)\n", + " \n", + "In CS220 we will usually give you a link to a copied file to avoid overloading the site.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## DEMO: Get Weather Data and play with it\n", + "\n", + "https://api.weather.gov/gridpoints/MKX/37,63/forecast" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['@context', 'type', 'geometry', 'properties'])" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "url = \"https://api.weather.gov/gridpoints/MKX/37,63/forecast\"\n", + "r = requests.get(url)\n", + "r.raise_for_status()\n", + "forecast_dict = r.json()\n", + "\n", + "# did I get something? \n", + "forecast_dict\n", + "\n", + "# keys only please\n", + "forecast_dict.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['updated', 'units', 'forecastGenerator', 'generatedAt', 'updateTime', 'validTimes', 'elevation', 'periods'])" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# go back to the web result...which key has the data I want? \n", + "forecast_dict['properties'].keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'number': 1,\n", + " 'name': 'Today',\n", + " 'startTime': '2022-11-16T08:00:00-06:00',\n", + " 'endTime': '2022-11-16T18:00:00-06:00',\n", + " 'isDaytime': True,\n", + " 'temperature': 34,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '5 to 10 mph',\n", + " 'windDirection': 'NW',\n", + " 'icon': 'https://api.weather.gov/icons/land/day/snow,60/snow,30?size=medium',\n", + " 'shortForecast': 'Snow Showers Likely',\n", + " 'detailedForecast': 'Snow showers likely. Cloudy, with a high near 34. Northwest wind 5 to 10 mph. Chance of precipitation is 60%. New snow accumulation of less than one inch possible.'},\n", + " {'number': 2,\n", + " 'name': 'Tonight',\n", + " 'startTime': '2022-11-16T18:00:00-06:00',\n", + " 'endTime': '2022-11-17T06:00:00-06:00',\n", + " 'isDaytime': False,\n", + " 'temperature': 23,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '5 to 10 mph',\n", + " 'windDirection': 'W',\n", + " 'icon': 'https://api.weather.gov/icons/land/night/snow?size=medium',\n", + " 'shortForecast': 'Chance Snow Showers',\n", + " 'detailedForecast': 'A chance of snow showers after 9pm. Mostly cloudy, with a low around 23. West wind 5 to 10 mph.'},\n", + " {'number': 3,\n", + " 'name': 'Thursday',\n", + " 'startTime': '2022-11-17T06:00:00-06:00',\n", + " 'endTime': '2022-11-17T18:00:00-06:00',\n", + " 'isDaytime': True,\n", + " 'temperature': 28,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '10 to 15 mph',\n", + " 'windDirection': 'W',\n", + " 'icon': 'https://api.weather.gov/icons/land/day/snow,30?size=medium',\n", + " 'shortForecast': 'Chance Snow Showers',\n", + " 'detailedForecast': 'A chance of snow showers. Mostly cloudy, with a high near 28. West wind 10 to 15 mph, with gusts as high as 25 mph. Chance of precipitation is 30%. New snow accumulation of less than half an inch possible.'},\n", + " {'number': 4,\n", + " 'name': 'Thursday Night',\n", + " 'startTime': '2022-11-17T18:00:00-06:00',\n", + " 'endTime': '2022-11-18T06:00:00-06:00',\n", + " 'isDaytime': False,\n", + " 'temperature': 17,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '15 mph',\n", + " 'windDirection': 'W',\n", + " 'icon': 'https://api.weather.gov/icons/land/night/bkn?size=medium',\n", + " 'shortForecast': 'Mostly Cloudy',\n", + " 'detailedForecast': 'Mostly cloudy, with a low around 17. West wind around 15 mph. New rainfall amounts less than a tenth of an inch possible.'},\n", + " {'number': 5,\n", + " 'name': 'Friday',\n", + " 'startTime': '2022-11-18T06:00:00-06:00',\n", + " 'endTime': '2022-11-18T18:00:00-06:00',\n", + " 'isDaytime': True,\n", + " 'temperature': 23,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '15 mph',\n", + " 'windDirection': 'W',\n", + " 'icon': 'https://api.weather.gov/icons/land/day/bkn?size=medium',\n", + " 'shortForecast': 'Mostly Cloudy',\n", + " 'detailedForecast': 'Mostly cloudy, with a high near 23. West wind around 15 mph.'},\n", + " {'number': 6,\n", + " 'name': 'Friday Night',\n", + " 'startTime': '2022-11-18T18:00:00-06:00',\n", + " 'endTime': '2022-11-19T06:00:00-06:00',\n", + " 'isDaytime': False,\n", + " 'temperature': 12,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '15 mph',\n", + " 'windDirection': 'SW',\n", + " 'icon': 'https://api.weather.gov/icons/land/night/bkn?size=medium',\n", + " 'shortForecast': 'Mostly Cloudy',\n", + " 'detailedForecast': 'Mostly cloudy, with a low around 12. Southwest wind around 15 mph.'},\n", + " {'number': 7,\n", + " 'name': 'Saturday',\n", + " 'startTime': '2022-11-19T06:00:00-06:00',\n", + " 'endTime': '2022-11-19T18:00:00-06:00',\n", + " 'isDaytime': True,\n", + " 'temperature': 23,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '15 mph',\n", + " 'windDirection': 'W',\n", + " 'icon': 'https://api.weather.gov/icons/land/day/bkn/snow,20?size=medium',\n", + " 'shortForecast': 'Mostly Cloudy then Slight Chance Snow Showers',\n", + " 'detailedForecast': 'A slight chance of snow showers after noon. Mostly cloudy, with a high near 23. West wind around 15 mph, with gusts as high as 25 mph. Chance of precipitation is 20%.'},\n", + " {'number': 8,\n", + " 'name': 'Saturday Night',\n", + " 'startTime': '2022-11-19T18:00:00-06:00',\n", + " 'endTime': '2022-11-20T06:00:00-06:00',\n", + " 'isDaytime': False,\n", + " 'temperature': 7,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '10 to 15 mph',\n", + " 'windDirection': 'W',\n", + " 'icon': 'https://api.weather.gov/icons/land/night/cold?size=medium',\n", + " 'shortForecast': 'Mostly Cloudy',\n", + " 'detailedForecast': 'Mostly cloudy, with a low around 7. West wind 10 to 15 mph, with gusts as high as 25 mph.'},\n", + " {'number': 9,\n", + " 'name': 'Sunday',\n", + " 'startTime': '2022-11-20T06:00:00-06:00',\n", + " 'endTime': '2022-11-20T18:00:00-06:00',\n", + " 'isDaytime': True,\n", + " 'temperature': 23,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '10 mph',\n", + " 'windDirection': 'W',\n", + " 'icon': 'https://api.weather.gov/icons/land/day/few?size=medium',\n", + " 'shortForecast': 'Sunny',\n", + " 'detailedForecast': 'Sunny, with a high near 23.'},\n", + " {'number': 10,\n", + " 'name': 'Sunday Night',\n", + " 'startTime': '2022-11-20T18:00:00-06:00',\n", + " 'endTime': '2022-11-21T06:00:00-06:00',\n", + " 'isDaytime': False,\n", + " 'temperature': 15,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': 'rising',\n", + " 'windSpeed': '10 mph',\n", + " 'windDirection': 'SW',\n", + " 'icon': 'https://api.weather.gov/icons/land/night/sct?size=medium',\n", + " 'shortForecast': 'Partly Cloudy',\n", + " 'detailedForecast': 'Partly cloudy. Low around 15, with temperatures rising to around 20 overnight.'},\n", + " {'number': 11,\n", + " 'name': 'Monday',\n", + " 'startTime': '2022-11-21T06:00:00-06:00',\n", + " 'endTime': '2022-11-21T18:00:00-06:00',\n", + " 'isDaytime': True,\n", + " 'temperature': 34,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': 'falling',\n", + " 'windSpeed': '5 to 10 mph',\n", + " 'windDirection': 'W',\n", + " 'icon': 'https://api.weather.gov/icons/land/day/few?size=medium',\n", + " 'shortForecast': 'Sunny',\n", + " 'detailedForecast': 'Sunny. High near 34, with temperatures falling to around 28 in the afternoon.'},\n", + " {'number': 12,\n", + " 'name': 'Monday Night',\n", + " 'startTime': '2022-11-21T18:00:00-06:00',\n", + " 'endTime': '2022-11-22T06:00:00-06:00',\n", + " 'isDaytime': False,\n", + " 'temperature': 18,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '5 mph',\n", + " 'windDirection': 'SW',\n", + " 'icon': 'https://api.weather.gov/icons/land/night/sct?size=medium',\n", + " 'shortForecast': 'Partly Cloudy',\n", + " 'detailedForecast': 'Partly cloudy, with a low around 18.'},\n", + " {'number': 13,\n", + " 'name': 'Tuesday',\n", + " 'startTime': '2022-11-22T06:00:00-06:00',\n", + " 'endTime': '2022-11-22T18:00:00-06:00',\n", + " 'isDaytime': True,\n", + " 'temperature': 36,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': 'falling',\n", + " 'windSpeed': '5 to 10 mph',\n", + " 'windDirection': 'SW',\n", + " 'icon': 'https://api.weather.gov/icons/land/day/sct?size=medium',\n", + " 'shortForecast': 'Mostly Sunny',\n", + " 'detailedForecast': 'Mostly sunny. High near 36, with temperatures falling to around 31 in the afternoon.'},\n", + " {'number': 14,\n", + " 'name': 'Tuesday Night',\n", + " 'startTime': '2022-11-22T18:00:00-06:00',\n", + " 'endTime': '2022-11-23T06:00:00-06:00',\n", + " 'isDaytime': False,\n", + " 'temperature': 26,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '5 mph',\n", + " 'windDirection': 'S',\n", + " 'icon': 'https://api.weather.gov/icons/land/night/bkn?size=medium',\n", + " 'shortForecast': 'Mostly Cloudy',\n", + " 'detailedForecast': 'Mostly cloudy, with a low around 26.'}]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# use two key to get the value we want\n", + "forecast_dict['properties']['periods']" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# what type is this? \n", + "type(forecast_dict['properties']['periods'])" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'number': 1,\n", + " 'name': 'Today',\n", + " 'startTime': '2022-11-16T08:00:00-06:00',\n", + " 'endTime': '2022-11-16T18:00:00-06:00',\n", + " 'isDaytime': True,\n", + " 'temperature': 34,\n", + " 'temperatureUnit': 'F',\n", + " 'temperatureTrend': None,\n", + " 'windSpeed': '5 to 10 mph',\n", + " 'windDirection': 'NW',\n", + " 'icon': 'https://api.weather.gov/icons/land/day/snow,60/snow,30?size=medium',\n", + " 'shortForecast': 'Snow Showers Likely',\n", + " 'detailedForecast': 'Snow showers likely. Cloudy, with a high near 34. Northwest wind 5 to 10 mph. Chance of precipitation is 60%. New snow accumulation of less than one inch possible.'}" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# what is the thing at index 0 ? \n", + "forecast_dict['properties']['periods'][0]" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "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>number</th>\n", + " <th>name</th>\n", + " <th>startTime</th>\n", + " <th>endTime</th>\n", + " <th>isDaytime</th>\n", + " <th>temperature</th>\n", + " <th>temperatureUnit</th>\n", + " <th>temperatureTrend</th>\n", + " <th>windSpeed</th>\n", + " <th>windDirection</th>\n", + " <th>icon</th>\n", + " <th>shortForecast</th>\n", + " <th>detailedForecast</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>0</th>\n", + " <td>1</td>\n", + " <td>Today</td>\n", + " <td>2022-11-16T08:00:00-06:00</td>\n", + " <td>2022-11-16T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>34</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>5 to 10 mph</td>\n", + " <td>NW</td>\n", + " <td>https://api.weather.gov/icons/land/day/snow,60...</td>\n", + " <td>Snow Showers Likely</td>\n", + " <td>Snow showers likely. Cloudy, with a high near ...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1</th>\n", + " <td>2</td>\n", + " <td>Tonight</td>\n", + " <td>2022-11-16T18:00:00-06:00</td>\n", + " <td>2022-11-17T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>23</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>5 to 10 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/night/snow?...</td>\n", + " <td>Chance Snow Showers</td>\n", + " <td>A chance of snow showers after 9pm. Mostly clo...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>2</th>\n", + " <td>3</td>\n", + " <td>Thursday</td>\n", + " <td>2022-11-17T06:00:00-06:00</td>\n", + " <td>2022-11-17T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>28</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>10 to 15 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/day/snow,30...</td>\n", + " <td>Chance Snow Showers</td>\n", + " <td>A chance of snow showers. Mostly cloudy, with ...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>3</th>\n", + " <td>4</td>\n", + " <td>Thursday Night</td>\n", + " <td>2022-11-17T18:00:00-06:00</td>\n", + " <td>2022-11-18T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>17</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>15 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/night/bkn?s...</td>\n", + " <td>Mostly Cloudy</td>\n", + " <td>Mostly cloudy, with a low around 17. West wind...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>4</th>\n", + " <td>5</td>\n", + " <td>Friday</td>\n", + " <td>2022-11-18T06:00:00-06:00</td>\n", + " <td>2022-11-18T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>23</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>15 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/day/bkn?siz...</td>\n", + " <td>Mostly Cloudy</td>\n", + " <td>Mostly cloudy, with a high near 23. West wind ...</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " number name startTime \\\n", + "0 1 Today 2022-11-16T08:00:00-06:00 \n", + "1 2 Tonight 2022-11-16T18:00:00-06:00 \n", + "2 3 Thursday 2022-11-17T06:00:00-06:00 \n", + "3 4 Thursday Night 2022-11-17T18:00:00-06:00 \n", + "4 5 Friday 2022-11-18T06:00:00-06:00 \n", + "\n", + " endTime isDaytime temperature temperatureUnit \\\n", + "0 2022-11-16T18:00:00-06:00 True 34 F \n", + "1 2022-11-17T06:00:00-06:00 False 23 F \n", + "2 2022-11-17T18:00:00-06:00 True 28 F \n", + "3 2022-11-18T06:00:00-06:00 False 17 F \n", + "4 2022-11-18T18:00:00-06:00 True 23 F \n", + "\n", + " temperatureTrend windSpeed windDirection \\\n", + "0 None 5 to 10 mph NW \n", + "1 None 5 to 10 mph W \n", + "2 None 10 to 15 mph W \n", + "3 None 15 mph W \n", + "4 None 15 mph W \n", + "\n", + " icon shortForecast \\\n", + "0 https://api.weather.gov/icons/land/day/snow,60... Snow Showers Likely \n", + "1 https://api.weather.gov/icons/land/night/snow?... Chance Snow Showers \n", + "2 https://api.weather.gov/icons/land/day/snow,30... Chance Snow Showers \n", + "3 https://api.weather.gov/icons/land/night/bkn?s... Mostly Cloudy \n", + "4 https://api.weather.gov/icons/land/day/bkn?siz... Mostly Cloudy \n", + "\n", + " detailedForecast \n", + "0 Snow showers likely. Cloudy, with a high near ... \n", + "1 A chance of snow showers after 9pm. Mostly clo... \n", + "2 A chance of snow showers. Mostly cloudy, with ... \n", + "3 Mostly cloudy, with a low around 17. West wind... \n", + "4 Mostly cloudy, with a high near 23. West wind ... " + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# OK, so this is a list of dicts\n", + "# can I read it into a Pandas DataFrame?\n", + "\n", + "forecast_df = pd.DataFrame(forecast_dict['properties']['periods'])\n", + "forecast_df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "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>name</th>\n", + " <th>startTime</th>\n", + " <th>endTime</th>\n", + " <th>isDaytime</th>\n", + " <th>temperature</th>\n", + " <th>temperatureUnit</th>\n", + " <th>temperatureTrend</th>\n", + " <th>windSpeed</th>\n", + " <th>windDirection</th>\n", + " <th>icon</th>\n", + " <th>shortForecast</th>\n", + " <th>detailedForecast</th>\n", + " </tr>\n", + " <tr>\n", + " <th>number</th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " <th></th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>1</th>\n", + " <td>Today</td>\n", + " <td>2022-11-16T08:00:00-06:00</td>\n", + " <td>2022-11-16T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>34</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>5 to 10 mph</td>\n", + " <td>NW</td>\n", + " <td>https://api.weather.gov/icons/land/day/snow,60...</td>\n", + " <td>Snow Showers Likely</td>\n", + " <td>Snow showers likely. Cloudy, with a high near ...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>2</th>\n", + " <td>Tonight</td>\n", + " <td>2022-11-16T18:00:00-06:00</td>\n", + " <td>2022-11-17T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>23</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>5 to 10 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/night/snow?...</td>\n", + " <td>Chance Snow Showers</td>\n", + " <td>A chance of snow showers after 9pm. Mostly clo...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>3</th>\n", + " <td>Thursday</td>\n", + " <td>2022-11-17T06:00:00-06:00</td>\n", + " <td>2022-11-17T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>28</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>10 to 15 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/day/snow,30...</td>\n", + " <td>Chance Snow Showers</td>\n", + " <td>A chance of snow showers. Mostly cloudy, with ...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>4</th>\n", + " <td>Thursday Night</td>\n", + " <td>2022-11-17T18:00:00-06:00</td>\n", + " <td>2022-11-18T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>17</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>15 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/night/bkn?s...</td>\n", + " <td>Mostly Cloudy</td>\n", + " <td>Mostly cloudy, with a low around 17. West wind...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>5</th>\n", + " <td>Friday</td>\n", + " <td>2022-11-18T06:00:00-06:00</td>\n", + " <td>2022-11-18T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>23</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>15 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/day/bkn?siz...</td>\n", + " <td>Mostly Cloudy</td>\n", + " <td>Mostly cloudy, with a high near 23. West wind ...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>6</th>\n", + " <td>Friday Night</td>\n", + " <td>2022-11-18T18:00:00-06:00</td>\n", + " <td>2022-11-19T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>12</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>15 mph</td>\n", + " <td>SW</td>\n", + " <td>https://api.weather.gov/icons/land/night/bkn?s...</td>\n", + " <td>Mostly Cloudy</td>\n", + " <td>Mostly cloudy, with a low around 12. Southwest...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>7</th>\n", + " <td>Saturday</td>\n", + " <td>2022-11-19T06:00:00-06:00</td>\n", + " <td>2022-11-19T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>23</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>15 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/day/bkn/sno...</td>\n", + " <td>Mostly Cloudy then Slight Chance Snow Showers</td>\n", + " <td>A slight chance of snow showers after noon. Mo...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>8</th>\n", + " <td>Saturday Night</td>\n", + " <td>2022-11-19T18:00:00-06:00</td>\n", + " <td>2022-11-20T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>7</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>10 to 15 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/night/cold?...</td>\n", + " <td>Mostly Cloudy</td>\n", + " <td>Mostly cloudy, with a low around 7. West wind ...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>9</th>\n", + " <td>Sunday</td>\n", + " <td>2022-11-20T06:00:00-06:00</td>\n", + " <td>2022-11-20T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>23</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>10 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/day/few?siz...</td>\n", + " <td>Sunny</td>\n", + " <td>Sunny, with a high near 23.</td>\n", + " </tr>\n", + " <tr>\n", + " <th>10</th>\n", + " <td>Sunday Night</td>\n", + " <td>2022-11-20T18:00:00-06:00</td>\n", + " <td>2022-11-21T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>15</td>\n", + " <td>F</td>\n", + " <td>rising</td>\n", + " <td>10 mph</td>\n", + " <td>SW</td>\n", + " <td>https://api.weather.gov/icons/land/night/sct?s...</td>\n", + " <td>Partly Cloudy</td>\n", + " <td>Partly cloudy. Low around 15, with temperature...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>11</th>\n", + " <td>Monday</td>\n", + " <td>2022-11-21T06:00:00-06:00</td>\n", + " <td>2022-11-21T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>34</td>\n", + " <td>F</td>\n", + " <td>falling</td>\n", + " <td>5 to 10 mph</td>\n", + " <td>W</td>\n", + " <td>https://api.weather.gov/icons/land/day/few?siz...</td>\n", + " <td>Sunny</td>\n", + " <td>Sunny. High near 34, with temperatures falling...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>12</th>\n", + " <td>Monday Night</td>\n", + " <td>2022-11-21T18:00:00-06:00</td>\n", + " <td>2022-11-22T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>18</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>5 mph</td>\n", + " <td>SW</td>\n", + " <td>https://api.weather.gov/icons/land/night/sct?s...</td>\n", + " <td>Partly Cloudy</td>\n", + " <td>Partly cloudy, with a low around 18.</td>\n", + " </tr>\n", + " <tr>\n", + " <th>13</th>\n", + " <td>Tuesday</td>\n", + " <td>2022-11-22T06:00:00-06:00</td>\n", + " <td>2022-11-22T18:00:00-06:00</td>\n", + " <td>True</td>\n", + " <td>36</td>\n", + " <td>F</td>\n", + " <td>falling</td>\n", + " <td>5 to 10 mph</td>\n", + " <td>SW</td>\n", + " <td>https://api.weather.gov/icons/land/day/sct?siz...</td>\n", + " <td>Mostly Sunny</td>\n", + " <td>Mostly sunny. High near 36, with temperatures ...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>14</th>\n", + " <td>Tuesday Night</td>\n", + " <td>2022-11-22T18:00:00-06:00</td>\n", + " <td>2022-11-23T06:00:00-06:00</td>\n", + " <td>False</td>\n", + " <td>26</td>\n", + " <td>F</td>\n", + " <td>None</td>\n", + " <td>5 mph</td>\n", + " <td>S</td>\n", + " <td>https://api.weather.gov/icons/land/night/bkn?s...</td>\n", + " <td>Mostly Cloudy</td>\n", + " <td>Mostly cloudy, with a low around 26.</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " name startTime endTime \\\n", + "number \n", + "1 Today 2022-11-16T08:00:00-06:00 2022-11-16T18:00:00-06:00 \n", + "2 Tonight 2022-11-16T18:00:00-06:00 2022-11-17T06:00:00-06:00 \n", + "3 Thursday 2022-11-17T06:00:00-06:00 2022-11-17T18:00:00-06:00 \n", + "4 Thursday Night 2022-11-17T18:00:00-06:00 2022-11-18T06:00:00-06:00 \n", + "5 Friday 2022-11-18T06:00:00-06:00 2022-11-18T18:00:00-06:00 \n", + "6 Friday Night 2022-11-18T18:00:00-06:00 2022-11-19T06:00:00-06:00 \n", + "7 Saturday 2022-11-19T06:00:00-06:00 2022-11-19T18:00:00-06:00 \n", + "8 Saturday Night 2022-11-19T18:00:00-06:00 2022-11-20T06:00:00-06:00 \n", + "9 Sunday 2022-11-20T06:00:00-06:00 2022-11-20T18:00:00-06:00 \n", + "10 Sunday Night 2022-11-20T18:00:00-06:00 2022-11-21T06:00:00-06:00 \n", + "11 Monday 2022-11-21T06:00:00-06:00 2022-11-21T18:00:00-06:00 \n", + "12 Monday Night 2022-11-21T18:00:00-06:00 2022-11-22T06:00:00-06:00 \n", + "13 Tuesday 2022-11-22T06:00:00-06:00 2022-11-22T18:00:00-06:00 \n", + "14 Tuesday Night 2022-11-22T18:00:00-06:00 2022-11-23T06:00:00-06:00 \n", + "\n", + " isDaytime temperature temperatureUnit temperatureTrend windSpeed \\\n", + "number \n", + "1 True 34 F None 5 to 10 mph \n", + "2 False 23 F None 5 to 10 mph \n", + "3 True 28 F None 10 to 15 mph \n", + "4 False 17 F None 15 mph \n", + "5 True 23 F None 15 mph \n", + "6 False 12 F None 15 mph \n", + "7 True 23 F None 15 mph \n", + "8 False 7 F None 10 to 15 mph \n", + "9 True 23 F None 10 mph \n", + "10 False 15 F rising 10 mph \n", + "11 True 34 F falling 5 to 10 mph \n", + "12 False 18 F None 5 mph \n", + "13 True 36 F falling 5 to 10 mph \n", + "14 False 26 F None 5 mph \n", + "\n", + " windDirection icon \\\n", + "number \n", + "1 NW https://api.weather.gov/icons/land/day/snow,60... \n", + "2 W https://api.weather.gov/icons/land/night/snow?... \n", + "3 W https://api.weather.gov/icons/land/day/snow,30... \n", + "4 W https://api.weather.gov/icons/land/night/bkn?s... \n", + "5 W https://api.weather.gov/icons/land/day/bkn?siz... \n", + "6 SW https://api.weather.gov/icons/land/night/bkn?s... \n", + "7 W https://api.weather.gov/icons/land/day/bkn/sno... \n", + "8 W https://api.weather.gov/icons/land/night/cold?... \n", + "9 W https://api.weather.gov/icons/land/day/few?siz... \n", + "10 SW https://api.weather.gov/icons/land/night/sct?s... \n", + "11 W https://api.weather.gov/icons/land/day/few?siz... \n", + "12 SW https://api.weather.gov/icons/land/night/sct?s... \n", + "13 SW https://api.weather.gov/icons/land/day/sct?siz... \n", + "14 S https://api.weather.gov/icons/land/night/bkn?s... \n", + "\n", + " shortForecast \\\n", + "number \n", + "1 Snow Showers Likely \n", + "2 Chance Snow Showers \n", + "3 Chance Snow Showers \n", + "4 Mostly Cloudy \n", + "5 Mostly Cloudy \n", + "6 Mostly Cloudy \n", + "7 Mostly Cloudy then Slight Chance Snow Showers \n", + "8 Mostly Cloudy \n", + "9 Sunny \n", + "10 Partly Cloudy \n", + "11 Sunny \n", + "12 Partly Cloudy \n", + "13 Mostly Sunny \n", + "14 Mostly Cloudy \n", + "\n", + " detailedForecast \n", + "number \n", + "1 Snow showers likely. Cloudy, with a high near ... \n", + "2 A chance of snow showers after 9pm. Mostly clo... \n", + "3 A chance of snow showers. Mostly cloudy, with ... \n", + "4 Mostly cloudy, with a low around 17. West wind... \n", + "5 Mostly cloudy, with a high near 23. West wind ... \n", + "6 Mostly cloudy, with a low around 12. Southwest... \n", + "7 A slight chance of snow showers after noon. Mo... \n", + "8 Mostly cloudy, with a low around 7. West wind ... \n", + "9 Sunny, with a high near 23. \n", + "10 Partly cloudy. Low around 15, with temperature... \n", + "11 Sunny. High near 34, with temperatures falling... \n", + "12 Partly cloudy, with a low around 18. \n", + "13 Mostly sunny. High near 36, with temperatures ... \n", + "14 Mostly cloudy, with a low around 26. " + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# how do you set 'number' to be the index ? \n", + "# let's do a Google Search\n", + "forecast_df = forecast_df.set_index('number')\n", + "forecast_df" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# what is the coldest forecasted temp? \n", + "forecast_df['temperature'].min()" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Sunny. High near 34, with temperatures falling to around 28 in the afternoon.'" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# what is the detailed forecast for Monday?\n", + "forecast_df [forecast_df['name'] == \"Monday\"]['detailedForecast'].iloc[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- GitLab