|
30 | 30 | - [<span class="toc-section-number">2.17</span> (Optional) Things we didn't talk about](#optional-things-we-didnt-talk-about) |
31 | 31 | - [<span class="toc-section-number">2.18</span> (Optional) Introspecting on the DataFrame object](#optional-introspecting-on-the-dataframe-object) |
32 | 32 | - [<span class="toc-section-number">3</span> Building Programs (Week 3)](#building-programs-week-3) |
33 | | - - [<span class="toc-section-number">3.1</span> Notebooks vs Python scripts](#notebooks-vs-python-scripts) |
34 | | - - [<span class="toc-section-number">3.2</span> (Optional) Python from the terminal](#optional-python-from-the-terminal) |
35 | | - - [<span class="toc-section-number">3.3</span> Looping Over Data Sets](#looping-over-data-sets) |
36 | | - - [<span class="toc-section-number">3.4</span> Conditionals](#conditionals) |
37 | | - - [<span class="toc-section-number">3.5</span> Writing Functions](#writing-functions) |
| 33 | + - [<span class="toc-section-number">3.1</span> Looping Over Data Sets](#looping-over-data-sets) |
| 34 | + - [<span class="toc-section-number">3.2</span> Conditionals](#conditionals) |
| 35 | + - [<span class="toc-section-number">3.3</span> Writing Functions](#writing-functions) |
| 36 | + - [<span class="toc-section-number">3.4</span> Notebooks vs Python scripts](#notebooks-vs-python-scripts) |
| 37 | + - [<span class="toc-section-number">3.5</span> (Optional) Python from the terminal](#optional-python-from-the-terminal) |
38 | 38 | - [<span class="toc-section-number">3.6</span> (Optional) Use pathlib to write code that works across operating systems](#optional-use-pathlib-to-write-code-that-works-across-operating-systems) |
39 | 39 | - [<span class="toc-section-number">3.7</span> (Optional) Generic file handling](#optional-generic-file-handling) |
40 | 40 | - [<span class="toc-section-number">3.8</span> (Optional) Text processing and data cleanup](#optional-text-processing-and-data-cleanup) |
@@ -1580,13 +1580,17 @@ data["wealthy"] = high_gdp |
1580 | 1580 |
|
1581 | 1581 | Capture the results of your filter in a new file, rather than overwriting your original data. |
1582 | 1582 |
|
1583 | | -``` python |
1584 | | -# Save to a new CSV, preserving your original data |
1585 | | -data.to_csv('gapminder_gdp_europe_grouped.csv') |
| 1583 | +1. Create a new subdirectory called "processed" using the editor's file interface. |
1586 | 1584 |
|
1587 | | -# If you don't want to preserve row names: |
1588 | | -data.to_csv('gapminder_gdp_europe_grouped.csv', index=False) |
1589 | | -``` |
| 1585 | +2. Save your data as a CSV in the subdirectory |
| 1586 | +
|
| 1587 | + ``` python |
| 1588 | + # Save to a new CSV, preserving your original data |
| 1589 | + data.to_csv('gapminder_gdp_europe_grouped.csv') |
| 1590 | +
|
| 1591 | + # If you don't want to preserve row names: |
| 1592 | + data.to_csv('gapminder_gdp_europe_grouped.csv', index=False) |
| 1593 | + ``` |
1590 | 1594 |
|
1591 | 1595 | ## Working with missing data |
1592 | 1596 |
|
@@ -2000,47 +2004,6 @@ Scikit-Learn documentation: <https://scikit-learn.org/stable/> |
2000 | 2004 |
|
2001 | 2005 | # Building Programs (Week 3) |
2002 | 2006 |
|
2003 | | -## Notebooks vs Python scripts |
2004 | | -
|
2005 | | -### Differences between .ipynb and .py |
2006 | | -
|
2007 | | -1. Export notebook to .py file |
2008 | | -2. Move .py file into data directory |
2009 | | -3. Compare files in TextEdit/Notepad |
2010 | | -
|
2011 | | -### Workflow differences between notebooks and scripts |
2012 | | -
|
2013 | | -Broadly, a trade-off between managing big code bases and making it easy to experiment. See: <https://github.com/elliewix/Ways-Of-Installing-Python/blob/master/ways-of-installing.md#why-do-you-need-a-specific-tool> |
2014 | | -
|
2015 | | -1. Interactive testing and debugging |
2016 | | -2. Graphics integration |
2017 | | -3. Version control |
2018 | | -4. Remote scripts |
2019 | | -
|
2020 | | -## (Optional) Python from the terminal |
2021 | | -
|
2022 | | -1. Python is an interactive interpreter (REPL) |
2023 | | -
|
2024 | | - ``` bash |
2025 | | - python |
2026 | | - ``` |
2027 | | -
|
2028 | | -2. Python is a command line program |
2029 | | -
|
2030 | | - ``` python |
2031 | | - # hello.py |
2032 | | - print("Hello!") |
2033 | | - ``` |
2034 | | -
|
2035 | | - ``` bash |
2036 | | - python hello.py |
2037 | | - ``` |
2038 | | -
|
2039 | | -3. (Optional) Python programs can accept command line arguments as inputs |
2040 | | -
|
2041 | | - 1. List of command line inputs: `sys.argv` (<https://docs.python.org/3/library/sys.html#sys.argv>) |
2042 | | - 2. Utility for working with arguments: `argparse` (<https://docs.python.org/3/library/argparse.html>) |
2043 | | -
|
2044 | 2007 | ## Looping Over Data Sets |
2045 | 2008 |
|
2046 | 2009 | ### File paths as an example of increasing abstraction in program development |
@@ -2349,8 +2312,10 @@ print_greeting() |
2349 | 2312 |
|
2350 | 2313 | ``` python |
2351 | 2314 | def print_date(year, month, day): |
2352 | | - joined = '/'.join([year, month, day]) |
2353 | | - print(joined) |
| 2315 | + """Print the formatted date. This works with strings or integers.""" |
| 2316 | +
|
| 2317 | + formatted_date = f"{year}/{month}/{day}" |
| 2318 | + print(formatted_date) |
2354 | 2319 |
|
2355 | 2320 | print_date(1871, 3, 19) |
2356 | 2321 | ``` |
@@ -2460,6 +2425,47 @@ all_data.to_csv("gapminder_mean_all.csv", index=False) |
2460 | 2425 |
|
2461 | 2426 | <https://matplotlib.org/stable/gallery/mplot3d/lorenz_attractor.html> |
2462 | 2427 |
|
| 2428 | +## Notebooks vs Python scripts |
| 2429 | +
|
| 2430 | +### Differences between .ipynb and .py |
| 2431 | +
|
| 2432 | +1. Export notebook to .py file |
| 2433 | +2. Move .py file into data directory |
| 2434 | +3. Compare files in TextEdit/Notepad |
| 2435 | +
|
| 2436 | +### Workflow differences between notebooks and scripts |
| 2437 | +
|
| 2438 | +Broadly, a trade-off between managing big code bases and making it easy to experiment. See: <https://github.com/elliewix/Ways-Of-Installing-Python/blob/master/ways-of-installing.md#why-do-you-need-a-specific-tool> |
| 2439 | +
|
| 2440 | +1. Interactive testing and debugging |
| 2441 | +2. Graphics integration |
| 2442 | +3. Version control |
| 2443 | +4. Remote scripts |
| 2444 | +
|
| 2445 | +## (Optional) Python from the terminal |
| 2446 | +
|
| 2447 | +1. Python is an interactive interpreter (REPL) |
| 2448 | +
|
| 2449 | + ``` bash |
| 2450 | + python |
| 2451 | + ``` |
| 2452 | +
|
| 2453 | +2. Python is a command line program |
| 2454 | +
|
| 2455 | + ``` python |
| 2456 | + # hello.py |
| 2457 | + print("Hello!") |
| 2458 | + ``` |
| 2459 | +
|
| 2460 | + ``` bash |
| 2461 | + python hello.py |
| 2462 | + ``` |
| 2463 | +
|
| 2464 | +3. (Optional) Python programs can accept command line arguments as inputs |
| 2465 | +
|
| 2466 | + 1. List of command line inputs: `sys.argv` (<https://docs.python.org/3/library/sys.html#sys.argv>) |
| 2467 | + 2. Utility for working with arguments: `argparse` (<https://docs.python.org/3/library/argparse.html>) |
| 2468 | +
|
2463 | 2469 | ## (Optional) Use pathlib to write code that works across operating systems |
2464 | 2470 |
|
2465 | 2471 | 1. Pathlib provides cross-platform path objects |
|
0 commit comments