Skip to content

Commit

Permalink
added gdrive to df code
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaylor committed Sep 12, 2024
1 parent 558e3cd commit fb1ffb3
Show file tree
Hide file tree
Showing 77 changed files with 1,313 additions and 949 deletions.
34 changes: 34 additions & 0 deletions course-materials/final_project.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,37 @@ Here are some links to potential data resources that you can use to develop your
- [Spotify Tracks](https://www.kaggle.com/datasets/zaheenhamidani/ultimate-spotify-tracks-db)
- [Lego Dataset](https://www.kaggle.com/datasets/rtatman/lego-database)

## Using Google Drive to store your .csv file.

Once you've found a .csv file that you want to use, you should:

1. Save your file to a google drive folder in your UCSB account.
2. Change the sharing settings to allow anyone with a link to view your file.
3. Open the sharing dialog and copy the sharing link to your clipboard.
4. Use the code below to download your file (you will need to add this code to the top of your notebook in the `Import Data` section)

:::{.callout-warning}
For this code to work on the workbench server, you will need to **switch your kernel from 3.10.0 to 3.7.13**. You can switch kernels by clicking on the kernel name in the upper right of your notebook.
:::
```{python}
import pandas as pd
import requests
def extract_file_id(url):
"""Extract file id from Google Drive Sharing URL."""
return url.split("/")[-2]
def df_from_gdrive_csv(url):
""" Get the CSV file from a Google Drive Sharing URL."""
file_id = extract_file_id(url)
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params={"id": file_id}, stream=True)
return pd.read_csv(response.raw)
# Example of how to use:
# Note: your sharing link will be different, but should look like this:
sharing_url = "https://drive.google.com/file/d/1RlilHNG7BtvXT2Pm4OpgNvEjVJJZNaps/view?usp=share_link"
df = df_from_gdrive_csv(sharing_url)
df.head()
```
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ <h3 class="anchored" data-anchor-id="task-1-list-operations">Task 1: List Operat
<li>Remove the second fruit from the list.</li>
<li>Print the final list.</li>
</ol>
<div id="89996fe3" class="cell" data-execution_count="1">
<div id="10bf1121" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Example code for instructor</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>fruits <span class="op">=</span> [<span class="st">"apple"</span>, <span class="st">"banana"</span>, <span class="st">"cherry"</span>]</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Original list:"</span>, fruits)</span>
Expand Down Expand Up @@ -512,7 +512,7 @@ <h3 class="anchored" data-anchor-id="task-2-dictionary-operations">Task 2: Dicti
<li>Update the quantity of an existing item.</li>
<li>Print the final inventory.</li>
</ol>
<div id="1a1973dc" class="cell" data-execution_count="2">
<div id="490b5322" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Example code for instructor</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>inventory <span class="op">=</span> {</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"apples"</span>: <span class="dv">50</span>,</span>
Expand Down Expand Up @@ -549,7 +549,7 @@ <h3 class="anchored" data-anchor-id="task-3-creating-and-manipulating-sets">Task
<li>Find and print the intersection of the two sets.</li>
<li>Add a new element to the <code>evens</code> set.</li>
</ol>
<div id="ccb08487" class="cell" data-execution_count="3">
<div id="d7e934c9" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Example code for instructor</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>evens <span class="op">=</span> {<span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">6</span>, <span class="dv">8</span>, <span class="dv">10</span>}</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>odds <span class="op">=</span> {<span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">5</span>, <span class="dv">7</span>, <span class="dv">9</span>}</span>
Expand Down Expand Up @@ -583,7 +583,7 @@ <h3 class="anchored" data-anchor-id="task-4-set-operations-vs.-list-operations">
<li>Use a list comprehension to remove duplicates.</li>
<li>Print the results of both methods.</li>
</ol>
<div id="48354c08" class="cell" data-execution_count="4">
<div id="24ecbb1f" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Example code for instructor</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>numbers <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">3</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">4</span>, <span class="dv">5</span>]</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
Expand Down
18 changes: 9 additions & 9 deletions docs/course-materials/answer-keys/3b_control_flows-key.html
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ <h3 class="anchored" data-anchor-id="task-1-simple-weather-advice">Task 1: Simpl
<li>Otherwise, print “Enjoy the pleasant weather!”</li>
</ul></li>
</ol>
<div id="33e17f14" class="cell" data-execution_count="1">
<div id="17bc6926" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>temperature <span class="op">=</span> <span class="dv">20</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div id="837e2720" class="cell" data-execution_count="2">
<div id="1eab0e4e" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> temperature <span class="op">&gt;</span> <span class="dv">25</span>:</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">"It's a hot day, stay hydrated!"</span>)</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="cf">else</span>:</span>
Expand All @@ -497,10 +497,10 @@ <h3 class="anchored" data-anchor-id="task-2-grade-classifier">Task 2: Grade Clas
<li>Below 60: “F”</li>
</ul></li>
</ol>
<div id="0c3cbcf2" class="cell" data-execution_count="3">
<div id="29120691" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>score <span class="op">=</span> <span class="dv">85</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div id="1d039084" class="cell" data-execution_count="4">
<div id="51d23454" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> score <span class="op">&gt;=</span> <span class="dv">90</span>:</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> grade <span class="op">=</span> <span class="st">'A'</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="cf">elif</span> score <span class="op">&gt;=</span> <span class="dv">80</span>:</span>
Expand Down Expand Up @@ -528,7 +528,7 @@ <h3 class="anchored" data-anchor-id="task-3-counting-sheep">Task 3: Counting She
<li>Use a for loop with the range() function</li>
<li>Print each number followed by “sheep”</li>
</ol>
<div id="5c6dd5d0" class="cell" data-execution_count="5">
<div id="745a32c2" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">6</span>):</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"</span><span class="sc">{</span>i<span class="sc">}</span><span class="ss"> sheep"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
Expand All @@ -548,10 +548,10 @@ <h3 class="anchored" data-anchor-id="task-4-sum-of-numbers">Task 4: Sum of Numbe
<li>Use a for loop with the range() function to add each number to <code>total</code></li>
<li>After the loop, print the total</li>
</ol>
<div id="b367073d" class="cell" data-execution_count="6">
<div id="0f3d865d" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>total <span class="op">=</span> <span class="dv">0</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div id="5d34a9e7" class="cell" data-execution_count="7">
<div id="1d8329a6" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">11</span>):</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> total <span class="op">=</span> total <span class="op">+</span> i</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a></span>
Expand All @@ -573,10 +573,10 @@ <h3 class="anchored" data-anchor-id="task-5-countdown">Task 5: Countdown</h3>
<li>After each print, decrease the countdown by 1</li>
<li>When the countdown reaches 0, print “Blast off!”</li>
</ol>
<div id="9d989e51" class="cell" data-execution_count="8">
<div id="65775175" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>countdown <span class="op">=</span> <span class="dv">5</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div id="beb8016c" class="cell" data-execution_count="9">
<div id="4d22dbae" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> countdown <span class="op">&gt;</span> <span class="dv">0</span>:</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(countdown)</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># (-= is a python syntax shortcut inherited from C)</span></span>
Expand Down
10 changes: 5 additions & 5 deletions docs/course-materials/answer-keys/3d_pandas_series-key.html
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ <h2 class="anchored" data-anchor-id="resources">Resources</h2>
<section id="setup" class="level2">
<h2 class="anchored" data-anchor-id="setup">Setup</h2>
<p>First, let’s import the necessary libraries and create a sample Series.</p>
<div id="0d8ce9dc" class="cell" data-execution_count="1">
<div id="bc2a5fe4" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
Expand All @@ -463,7 +463,7 @@ <h2 class="anchored" data-anchor-id="exercise-1-creating-a-series">Exercise 1: C
<blockquote class="blockquote">
<p>apple: $0.5, banana: $0.3, cherry: $1.0, date: $1.5, elderberry: $2.0</p>
</blockquote>
<div id="e37faf12" class="cell" data-execution_count="2">
<div id="31594d36" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Create a Series called 'prices' with the same index as 'fruits'</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Use these prices: apple: $0.5, banana: $0.3, cherry: $1.0, date: $1.5, elderberry: $2.0</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>prices <span class="op">=</span> pd.Series([<span class="fl">0.5</span>, <span class="fl">0.3</span>, <span class="fl">1.0</span>, <span class="fl">2.5</span>, <span class="fl">3.0</span>], index<span class="op">=</span>fruits.values, name<span class="op">=</span><span class="st">'Prices'</span>)</span>
Expand All @@ -486,7 +486,7 @@ <h2 class="anchored" data-anchor-id="exercise-2-series-operations">Exercise 2: S
<li>Find the most expensive fruit.</li>
<li>Apply a 10% discount to all fruits priced over 1.0.</li>
</ol>
<div id="e74d8518" class="cell" data-execution_count="3">
<div id="b488d848" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 1. Calculate the total price of all fruits</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>total_price <span class="op">=</span> prices.<span class="bu">sum</span>()</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a></span>
Expand Down Expand Up @@ -522,7 +522,7 @@ <h2 class="anchored" data-anchor-id="exercise-3-series-analysis">Exercise 3: Ser
<li>How many fruits cost less than $1.0?</li>
<li>What is the price range (difference between max and min prices)?</li>
</ol>
<div id="5f54e56a" class="cell" data-execution_count="4">
<div id="7cdd0fd9" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 1. Calculate the average price of the fruits</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>average_price <span class="op">=</span> prices.mean()</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
Expand Down Expand Up @@ -550,7 +550,7 @@ <h2 class="anchored" data-anchor-id="exercise-4-series-manipulation">Exercise 4:
<li>Remove ‘banana’ from both Series.</li>
<li>Sort both Series by fruit name (alphabetically).</li>
</ol>
<div id="8894927c" class="cell" data-execution_count="5">
<div id="2af870ab" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 1. Add 'fig' to both Series (price: $1.2)</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>fruits <span class="op">=</span> pd.concat([fruits, pd.Series([<span class="st">'fig'</span>], name<span class="op">=</span><span class="st">'Fruits'</span>)])</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>prices <span class="op">=</span> pd.concat([prices, pd.Series([<span class="fl">1.2</span>], index<span class="op">=</span>[<span class="st">'fig'</span>], name<span class="op">=</span><span class="st">'Prices'</span>)])</span>
Expand Down
Loading

0 comments on commit fb1ffb3

Please sign in to comment.