Skip to content

Commit e1de796

Browse files
committed
minor edits
1 parent 995278a commit e1de796

File tree

4 files changed

+90
-65
lines changed

4 files changed

+90
-65
lines changed

lessons/1_Functions_and_Conditionals.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
"* An indented block of code denotes the start of the *body*.\n",
219219
"* The final line should be a `return` statement with the value(s) to be returned from the function.\n",
220220
"\n",
221-
"Let's take a look at a simple function:"
221+
"Let's take a look at a simple function that converts feet into meters:"
222222
]
223223
},
224224
{
@@ -636,15 +636,15 @@
636636
"\n",
637637
"Say that we want to create a new column in our dataset that classifies our datapoints in terms of the level of development, as measured by per capita gross national income (GNI). [This UN document](https://www.un.org/en/development/desa/policy/wesp/wesp_current/2014wesp_country_classification.pdf) outlines some rules for this.\n",
638638
"\n",
639-
"Here's what you need to do:\n",
639+
"A good way to approach these kinds of problems is to write down all the steps you need to take. Then, you write your code by following the steps. In this case, we need to do the following:\n",
640640
"\n",
641641
"1. Start a function called `assign_level` that takes in one parameter, `i`.\n",
642642
"2. Write an if-elif-else statement that checks `i`, based on the following rules:\n",
643-
" - If it is more than 12615, `return` the string `high-income`. \n",
644-
" - If it is more than 4086, `return` the string `upper middle income`. \n",
645-
" - If it is more than 1035, `return` the string `lower middle income`. \n",
646-
" - If it is less than or equal to 1035, `return` the string `low-income`. \n",
647-
" - Else, return `np.nan` (this is a NaN value).\n",
643+
" - `if` it is more than 12615, `return` the string `high-income`. \n",
644+
" - `elif` it is more than 4086, `return` the string `upper middle income`. \n",
645+
" - `elif` it is more than 1035, `return` the string `lower middle income`. \n",
646+
" - `elif` it is less than or equal to 1035, `return` the string `low-income`. \n",
647+
" - `else`, return `np.nan` (this is a NaN value).\n",
648648
"3. Use `.apply()` on the `gniPercap` column, using your new `assign_level` function as the argument. Assign the output to a new column in our DataFrame, called `income_level`."
649649
]
650650
},
@@ -663,7 +663,7 @@
663663
"cell_type": "markdown",
664664
"metadata": {},
665665
"source": [
666-
"If you've done this correctly, the following code should produce a barplot of the different income levels in our data."
666+
"If you've done this correctly, the following code should produce a barplot of the different income levels in our data!"
667667
]
668668
},
669669
{

lessons/2_Iteration_and_Visualization.ipynb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@
9797
"\n",
9898
"<img src=\"../images/for.png\" alt=\"For loop in Python\" width=\"700\"/>\n",
9999
"\n",
100-
"Pay attention to the **loop variable** (`lifeExp`). It stands for each item in the list (`lifeExp_list`) we are iterating through. Loop variables: \n",
101-
" - Are created on demand.\n",
102-
" - Only exist inside the loop.\n",
103-
" - Can have any name. "
100+
"Pay attention to the **loop variable** (`lifeExp`). It stands for each item in the list (`lifeExp_list`) we are iterating through. Loop variables can have any name; if we'd change it to `x`, it would still work. However, loop variables only exist inside the loop.\n",
101+
"\n",
102+
"🔔 **Question**: Would you prefer `lifeExp` or `x` as a name for the loop variable? Why?"
104103
]
105104
},
106105
{

lessons/3_Project.ipynb

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"\n",
3030
"### Sections\n",
3131
"1. [🚀 Project](#project)\n",
32-
"2. [Import the Data](#data)\n",
33-
"3. [Data Cleaning](#clean)\n",
34-
"4. [Exploratory Data Analysis](#eda)\n"
32+
"2. [Step 1: Import the Data](#data)\n",
33+
"3. [Step 2: Data Cleaning](#clean)\n",
34+
"4. [Step 3: Data Analysis](#eda)\n"
3535
]
3636
},
3737
{
@@ -40,7 +40,6 @@
4040
"source": [
4141
"<a id='project'></a>\n",
4242
"\n",
43-
"\n",
4443
"# 🚀 Project\n",
4544
"\n",
4645
"### Data: California Health Interview Survey\n",
@@ -55,20 +54,9 @@
5554
"- `household_tenure`: Self-Reported household tenure\n",
5655
"- `interview_language`: Language of interview\n",
5756
"\n",
58-
"We will bring together the basic programming, loading data, and statistical analysis/visualization techniques from this workshop to analyze this data. \n",
57+
"For this 🚀 Project, the goal we want to accomplish is **visualizing the relationship between poverty level and general health**. We will bring together basic programming and data science techniques you have learned to do this.\n",
5958
"\n",
60-
"First, let's import the packages to use in this analysis:"
61-
]
62-
},
63-
{
64-
"cell_type": "code",
65-
"execution_count": null,
66-
"metadata": {},
67-
"outputs": [],
68-
"source": [
69-
"import numpy as np\n",
70-
"import pandas as pd\n",
71-
"import os"
59+
"🔔 **Question**: Are there other research questions you could imagine asking with this dataset?"
7260
]
7361
},
7462
{
@@ -79,7 +67,7 @@
7967
"source": [
8068
"<a id='data'></a>\n",
8169
"\n",
82-
"# Import the Data \n",
70+
"# Step 1: Importing Data \n",
8371
"\n",
8472
"Before we import our data, a few words on **filepaths**. \n",
8573
"\n",
@@ -139,7 +127,7 @@
139127
"tags": []
140128
},
141129
"source": [
142-
"## 🥊 Challenge 1: Find the Data\n",
130+
"## Locating the Data\n",
143131
"\n",
144132
"Try to locate the files in the \"chis_data\" folder, which is in the \"data\" folder, which is in the main \"Python-Fundamentals\" folder. Using `pd.read_csv()`, read in all three data frames and assign them to the three variables defined below.\n",
145133
"\n",
@@ -156,6 +144,8 @@
156144
"metadata": {},
157145
"outputs": [],
158146
"source": [
147+
"import pandas as pd \n",
148+
"\n",
159149
"# YOUR CODE HERE\n",
160150
"df_eng = ...\n",
161151
"df_esp = ...\n",
@@ -168,7 +158,7 @@
168158
"tags": []
169159
},
170160
"source": [
171-
"## 🥊 Challenge 2: Concatenate\n",
161+
"## Concatenating DataFrames\n",
172162
"\n",
173163
"Look up the [documentation for Pandas](https://pandas.pydata.org/pandas-docs/stable/reference/general_functions.html), and see if you can find a function that **concatenates** the three DataFrames we have now. Save the concatenated list in a new variable called `df`."
174164
]
@@ -208,16 +198,14 @@
208198
"source": [
209199
"<a id='clean'></a>\n",
210200
"\n",
211-
"# Data Cleaning"
201+
"# Step 2: Data Cleaning"
212202
]
213203
},
214204
{
215205
"cell_type": "markdown",
216206
"metadata": {},
217207
"source": [
218-
"## 🥊 Challenge 3: Data Cleaning \n",
219-
"\n",
220-
"Often, we will want to remove some missing values in a data frame. Have a look at the `general_health` column and find the missing values using the `.isna()` method. Then, use `.sum()` to sum the amount of undefined (NaN) values."
208+
"Often, we will want to remove some missing values in a DataFrame. Have a look at the `general_health` column and find the missing values using the `.isna()` method. Then, use `.sum()` to sum the amount of undefined (NaN) values."
221209
]
222210
},
223211
{
@@ -253,11 +241,12 @@
253241
"source": [
254242
"<a id='eda'></a>\n",
255243
"\n",
256-
"# Exploratory Data Analysis\n",
244+
"# Step 3: Data Analysis\n",
257245
"\n",
246+
"Now that we have preprocessed data, we want to analyze it. Recall that our goal is to visualize a relationship between poverty level and general health. Before we do this, we should get a better grasp of what is in our data.\n",
258247
"\n",
259-
"## 🥊 Challenge 4: Count Values\n",
260-
"The first thing we will want to do is count values of some features. \n",
248+
"## Counting Values\n",
249+
"The first thing we will want to do is count values of poverty levels: we want to see how many levels there are, and how the data are distributed. \n",
261250
"1. Run `value_counts()` on the `poverty_level` column. \n",
262251
"2. <span style=\"color:purple\"> Look through the [documentation](https://pandas.pydata.org/docs/reference/api/pandas.Series.value_counts.html) and **normalize** the output of `value_counts()`.</span>"
263252
]
@@ -277,9 +266,11 @@
277266
"tags": []
278267
},
279268
"source": [
280-
"## 🥊 Challenge 5: Create a Function\n",
269+
"## Creating a Function\n",
270+
"\n",
271+
"It turns out that poverty is expressed \"as Times of 100% Federal Poverty Line (FPL)\". One approach to this could be to see if we can find differences in general health for people **below and above the poverty line**. \n",
281272
"\n",
282-
"Let's see if we can find differences in general health for people **below and above the poverty line**. First, let's create a function that can check whether this is the case.\n",
273+
"To do this, we can create a function that takes in values of the `poverty_level` column and outputs whether that value is above or below the poverty line.\n",
283274
"\n",
284275
"1. Create a new function called `assign_level`. It takes one parameter, which we'll call `i`.\n",
285276
"2. If `i` is `0-99% FPL`, return 0. In all other cases, return 1."
@@ -298,9 +289,9 @@
298289
"cell_type": "markdown",
299290
"metadata": {},
300291
"source": [
301-
"## 🥊 Challenge 6: Apply a Function\n",
292+
"## Applying a Function\n",
302293
"\n",
303-
"Using the function we created and `apply()` method, we want to create a new column in our DataFrame. \n",
294+
"Recall that we can use the `apply()` method in Pandas to apply our new function to the `poverty_level` column of our DataFrame. We also want to save the output of this `apply()` method to a new column in our DataFrame. \n",
304295
"\n",
305296
"1. Use the `apply()` method on the `poverty_level` column. Pass your `assign_level` function as the argument.\n",
306297
"3. Save the result of this operation in a new column in your `df`, called `above_poverty_line`."
@@ -319,9 +310,17 @@
319310
"cell_type": "markdown",
320311
"metadata": {},
321312
"source": [
322-
"## 🥊 Challenge 7: Subset DataFrames\n",
313+
"## Subsetting a DataFrame\n",
323314
"\n",
324-
"We want to create two bar plots of general health – for people above and below the poverty line. \n",
315+
"In order to create two bar plots of general health – for people above and below the poverty line – we can create two DataFrames for these groups. We can then plot the values in these DataFrames \"on top of\" one another in a barplot.\n",
316+
"\n",
317+
"Recall that we can subset DataFrames with Boolean masks. For instance, say we have a DataFrame `counts` with a column `A`. If we want to create a new DataFrame called `above_800`, which only contains the values over 800 in column `A` of `counts`, we would write:\n",
318+
"\n",
319+
"```\n",
320+
"above_800 = counts[counts['A'] > 800]\n",
321+
"```\n",
322+
"\n",
323+
"Let's perform the same operation on our data.\n",
325324
"\n",
326325
"1. Create a new DataFrame, `df_below`. It will be a subset of our `df`, based on the condition that the value in `above_poverty_line` is 0.\n",
327326
"2. Create a new DataFrame, `df_above`. It will be a subset of our `df`, based on the condition that the value in `above_poverty_line` is 1."
@@ -342,11 +341,13 @@
342341
"cell_type": "markdown",
343342
"metadata": {},
344343
"source": [
345-
"## 🥊 Challenge 8: Bar Plot of Value Counts\n",
344+
"## Creating the Visualization\n",
345+
"\n",
346+
"Finally, let's create our bar plots. We will create 2 plots in 1 cell, which will be plotted on top of one another. \n",
346347
"\n",
347-
"Finally, let's create our bar plots. Fill in the blanks below, following the steps. \n",
348+
"Fill in the blanks below, following the steps. \n",
348349
"\n",
349-
"1. Run a **normalized** `value_counts()` on the `general_health` column of `df_above`.\n",
350+
"1. Run a **normalized** `value_counts()` on the `general_health` column of `df_above` and `df_below`.\n",
350351
"2. Run `plot()` on the output of the resulting DataFrame. Enter the values for two arguments: `kind` must be set to `bar`, and `alpha` must be set to `.5`.\n"
351352
]
352353
},
@@ -360,7 +361,7 @@
360361
"source": [
361362
"# YOUR CODE HERE\n",
362363
"df_above[...].value_counts(...).plot(kind=..., alpha=...);\n",
363-
"df_below['general_health'].value_counts(normalize=True).plot(kind=..., alpha=...,color='maroon');"
364+
"df_below[...].value_counts(...).plot(kind=..., alpha=...,color='maroon');"
364365
]
365366
},
366367
{

solutions/3_Project.ipynb

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"tags": []
1818
},
1919
"source": [
20-
"## 🥊 Challenge 1: Find the Data\n",
20+
"## Locating the Data\n",
2121
"\n",
2222
"Try to locate the files in the \"chis_data\" folder, which is in the \"data\" folder, which is in the main \"Python-Fundamentals\" folder. Using `pd.read_csv()`, read in all three data frames and assign them to the three variables defined below.\n",
2323
"\n",
@@ -69,7 +69,7 @@
6969
"tags": []
7070
},
7171
"source": [
72-
"## 🥊 Challenge 2: Concatenate\n",
72+
"## Concatenating DataFrames\n",
7373
"\n",
7474
"Look up the [documentation for Pandas](https://pandas.pydata.org/pandas-docs/stable/reference/general_functions.html), and see if you can find a function that **concatenates** the three DataFrames we have now. Save the concatenated list in a new variable called `df`."
7575
]
@@ -214,8 +214,15 @@
214214
"cell_type": "markdown",
215215
"metadata": {},
216216
"source": [
217-
"## 🥊 Challenge 3: Data Cleaning \n",
217+
"<a id='clean'></a>\n",
218218
"\n",
219+
"# Step 2: Data Cleaning"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {},
225+
"source": [
219226
"Often, we will want to remove some missing values in a data frame. Have a look at the `general_health` column and find the missing values using the `.isna()` method. Then, use `.sum()` to sum the amount of undefined (NaN) values."
220227
]
221228
},
@@ -263,10 +270,16 @@
263270
"cell_type": "markdown",
264271
"metadata": {},
265272
"source": [
266-
"## 🥊 Challenge 4: Count Values\n",
267-
"The first thing we will want to do is count values of some features. \n",
273+
"<a id='eda'></a>\n",
274+
"\n",
275+
"# Step 3: Data Analysis\n",
276+
"\n",
277+
"Now that we have preprocessed data, we want to analyze it. Recall that our goal is to visualize a relationship between poverty level and general health. Before we do this, we should get a better grasp of what is in our data.\n",
278+
"\n",
279+
"## Counting Values\n",
280+
"The first thing we will want to do is count values of poverty levels: we want to see how many levels there are, and how the data are distributed. \n",
268281
"1. Run `value_counts()` on the `poverty_level` column. \n",
269-
"2. Look through the [documentation](https://pandas.pydata.org/docs/reference/api/pandas.Series.value_counts.html) and **normalize** the output of `value_counts()`."
282+
"2. <span style=\"color:purple\"> Look through the [documentation](https://pandas.pydata.org/docs/reference/api/pandas.Series.value_counts.html) and **normalize** the output of `value_counts()`.</span>"
270283
]
271284
},
272285
{
@@ -300,9 +313,11 @@
300313
"tags": []
301314
},
302315
"source": [
303-
"## 🥊 Challenge 5: Create a Function\n",
316+
"## Creating a Function\n",
304317
"\n",
305-
"Let's see if we can find differences in general health for people **below and above the poverty line**. First, let's create a function that can check whether this is the case.\n",
318+
"It turns out that poverty is expressed \"as Times of 100% Federal Poverty Line (FPL)\". One approach to this could be to see if we can find differences in general health for people **below and above the poverty line**. \n",
319+
"\n",
320+
"To do this, we can create a function that takes in values of the `poverty_level` column and outputs whether that value is above or below the poverty line.\n",
306321
"\n",
307322
"1. Create a new function called `assign_level`. It takes one parameter, which we'll call `i`.\n",
308323
"2. If `i` is `0-99% FPL`, return 0. In all other cases, return 1."
@@ -326,9 +341,9 @@
326341
"cell_type": "markdown",
327342
"metadata": {},
328343
"source": [
329-
"## 🥊 Challenge 6: Apply a Function\n",
344+
"## Applying a Function\n",
330345
"\n",
331-
"Using the function we created and `apply()` method, we want to create a new column in our DataFrame. \n",
346+
"Recall that we can use the `apply()` method in Pandas to apply our new function to the `poverty_level` column of our DataFrame. We also want to save the output of this `apply()` method to a new column in our DataFrame. \n",
332347
"\n",
333348
"1. Use the `apply()` method on the `poverty_level` column. Pass your `assign_level` function as the argument.\n",
334349
"3. Save the result of this operation in a new column in your `df`, called `above_poverty_line`."
@@ -348,9 +363,17 @@
348363
"cell_type": "markdown",
349364
"metadata": {},
350365
"source": [
351-
"## 🥊 Challenge 7: Subset DataFrames\n",
366+
"## Subsetting a DataFrame\n",
367+
"\n",
368+
"In order to create two bar plots of general health – for people above and below the poverty line – we can create two DataFrames for these groups. We can then plot the values in these DataFrames in a barplot.\n",
352369
"\n",
353-
"We want to create two bar plots of general health – for people above and below the poverty line. \n",
370+
"Recall that we can subset DataFrames with Boolean masks. For instance, say we have a DataFrame `counts` with a column `A`. If we want to create a new DataFrame called `above_800`, which only contains the values over 800 in column `A` of `counts`, we would write:\n",
371+
"\n",
372+
"```\n",
373+
"above_800 = counts[counts['A'] > 800]\n",
374+
"```\n",
375+
"\n",
376+
"Let's perform the same operation on our data.\n",
354377
"\n",
355378
"1. Create a new DataFrame, `df_below`. It will be a subset of our `df`, based on the condition that the value in `above_poverty_line` is 0.\n",
356379
"2. Create a new DataFrame, `df_above`. It will be a subset of our `df`, based on the condition that the value in `above_poverty_line` is 1."
@@ -371,12 +394,14 @@
371394
"cell_type": "markdown",
372395
"metadata": {},
373396
"source": [
374-
"## 🥊 Challenge 8: Bar Plot of Value Counts\n",
397+
"## Creating the Visualization\n",
398+
"\n",
399+
"Finally, let's create our bar plots. We will create 2 plots in 1 cell, which will be plotted on top of one another. \n",
375400
"\n",
376-
"Finally, let's create our bar plots. Fill in the blanks below, following the steps.\n",
401+
"Fill in the blanks below, following the steps. \n",
377402
"\n",
378-
"1. Run a **normalized** `value_counts()` on the `general_health` column of `df_above`.\n",
379-
"2. Run `plot()` on the output of the resulting DataFrame. Enter the values for two arguments: `kind` must be set to `bar`, and `alpha` must be set to `.5`."
403+
"1. Run a **normalized** `value_counts()` on the `general_health` column of `df_above` and `df_below`.\n",
404+
"2. Run `plot()` on the output of the resulting DataFrame. Enter the values for two arguments: `kind` must be set to `bar`, and `alpha` must be set to `.5`.\n"
380405
]
381406
},
382407
{

0 commit comments

Comments
 (0)