|
13 | 13 | "### Learning Objectives \n",
|
14 | 14 | " \n",
|
15 | 15 | "* Implement loops to do repeated computations.\n",
|
16 |
| - "* Understand how to use vectorization in Pandas.\n", |
| 16 | + "* Understand how to implement loops in Pandas with a technique called \"vectorization\".\n", |
17 | 17 | "* Apply several Pandas methods to summarize and manipulate data.\n",
|
18 | 18 | "* Distinguish Pandas methods for `DataFrame` and `Series` objects.\n",
|
19 | 19 | "* Create simple visualizations using Pandas. \n",
|
|
34 | 34 | "4. [Methods for `Series` Objects](#series_meth)"
|
35 | 35 | ]
|
36 | 36 | },
|
37 |
| - { |
38 |
| - "cell_type": "code", |
39 |
| - "execution_count": null, |
40 |
| - "metadata": {}, |
41 |
| - "outputs": [], |
42 |
| - "source": [ |
43 |
| - "import pandas as pd\n", |
44 |
| - "import numpy as np\n", |
45 |
| - "import matplotlib.pyplot as plt" |
46 |
| - ] |
47 |
| - }, |
48 | 37 | {
|
49 | 38 | "cell_type": "markdown",
|
50 | 39 | "metadata": {},
|
|
55 | 44 | "\n",
|
56 | 45 | "The strength of using computers is their speed. We can leverage this through repeated computation, also called iteration. In Python, we can do this using **loops**. \n",
|
57 | 46 | "\n",
|
58 |
| - "A **[for loop](https://www.w3schools.com/python/python_for_loops.asp)** executes some statements once *for* each value in an interable (like a list or a string). It says: \"*for* each thing in this group, *do* these operations\".\n", |
| 47 | + "A **[for loop](https://www.w3schools.com/python/python_for_loops.asp)** executes some statements once *for* each value in an iterable (like a list or a string). It says: \"*for* each thing in this group, *do* these operations\".\n", |
59 | 48 | "\n",
|
60 | 49 | "Let's take a look at the syntax of a for loop using the above example:"
|
61 | 50 | ]
|
|
182 | 171 | "In the above example, we are operating on each value in `numbers`. However, instead of simply printing the results, we often will want to save them somehow. We can do this with an **accumulator variable**.\n",
|
183 | 172 | "\n",
|
184 | 173 | "A common strategy in programs is to:\n",
|
185 |
| - "1. Initialize an *accumulator* variable appropriate to the datatype of the output:\n", |
| 174 | + "1. Initialize an accumulator variable appropriate to the datatype of the output:\n", |
186 | 175 | " * `int` : `0`\n",
|
187 | 176 | " * `str` : `''`\n",
|
188 | 177 | " * `list` : `[]`\n",
|
|
286 | 275 | "print(result)"
|
287 | 276 | ]
|
288 | 277 | },
|
| 278 | + { |
| 279 | + "cell_type": "markdown", |
| 280 | + "metadata": {}, |
| 281 | + "source": [ |
| 282 | + "💡 **Tip**: You might also encounter **[while loops](https://www.w3schools.com/python/python_while_loops.asp)**. A while loop says: \"*while* Condition A is true, *do* these operations\". We don't use these loops frequently in this type of programming so we won't cover them here." |
| 283 | + ] |
| 284 | + }, |
289 | 285 | {
|
290 | 286 | "cell_type": "markdown",
|
291 | 287 | "metadata": {},
|
|
397 | 393 | "# YOUR CODE HERE\n"
|
398 | 394 | ]
|
399 | 395 | },
|
400 |
| - { |
401 |
| - "cell_type": "markdown", |
402 |
| - "metadata": {}, |
403 |
| - "source": [ |
404 |
| - "💡 **Tip** You might also encounter **[while loops](https://www.w3schools.com/python/python_while_loops.asp)**. A while loop says: \"*while* Condition A is true, *do* these operations\". We don't use these loops frequently in this type of programming so we won't cover them here." |
405 |
| - ] |
406 |
| - }, |
407 | 396 | {
|
408 | 397 | "cell_type": "markdown",
|
409 | 398 | "metadata": {},
|
|
515 | 504 | "\n",
|
516 | 505 | "Bar plots show the relationship between a numeric and a categoric variable. Here, we use the `country` (categorical) and `lifeExp` (numeric) columns. Use a bar plot when you want to illustrate differences in frequencies of some category.\n",
|
517 | 506 | "\n",
|
518 |
| - "In the below cell, we retrieve the 10 data points with the **lowest life expectancy** in our data using the `sort_values()` method, and then plot those data points in a bar plot." |
| 507 | + "In the below cell, we retrieve the 10 data points with the **lowest life expectancy** in our data using the `sort_values()` method, and then plot those data points in a bar plot.\n", |
| 508 | + "\n", |
| 509 | + "💡 **Tip**: Note that `plot.bar()` is a method of its own, and is an alternative to using `plot()` with the `type=bar` argument." |
519 | 510 | ]
|
520 | 511 | },
|
521 | 512 | {
|
|
527 | 518 | "# Sort values based on low life expectancy, get top 10\n",
|
528 | 519 | "low_lifeExp = df.sort_values('lifeExp', ascending=True)[:10]\n",
|
529 | 520 | "\n",
|
530 |
| - "# Visualize with bar plot\n", |
| 521 | + "# Visualize with bar plot \n", |
531 | 522 | "low_lifeExp.plot.bar(x='country', y='lifeExp', figsize=(6,4));"
|
532 | 523 | ]
|
533 | 524 | },
|
|
696 | 687 | "\n",
|
697 | 688 | "## ❗ Key Points\n",
|
698 | 689 | "\n",
|
699 |
| - "* A `for` loop executes some statements once for each value in an interable.\n", |
700 | 690 | "* `for` loops work on lists and other list-like structures, but also on other iterables such as strings.\n",
|
701 |
| - "* We typically use an aggregator variable to store some information we retrieve using a `for` loop. \n", |
702 |
| - "* The `.describe()` method in Pandas summarizes numerical data in a dataset.\n", |
703 |
| - "* We typically do not want to use for-loops in Pandas - instead, we use \"vectorized\" operations.\n", |
704 |
| - "* The `.plot()` method in Pandas takes a `kind=` argument that determines what kind of plot it is - such as `scatter` or `hist`.\n", |
| 691 | + "* We typically use an accumulator variable to store some information we retrieve using a `for` loop. \n", |
| 692 | + "* We typically do not use for-loops in Pandas - instead, we use \"vectorized\" operations.\n", |
| 693 | + "* Pandas methods work on either `DataFrame` or `Series` objects--make sure you know which!\n", |
| 694 | + "* Pandas methods yield as output either `DataFrame` or `Series` objects--make sure you know which!\n", |
705 | 695 | "\n",
|
706 | 696 | "</div>"
|
707 | 697 | ]
|
|
0 commit comments