Skip to content

Commit 9448bbb

Browse files
committed
complete module1
1 parent 63f8570 commit 9448bbb

27 files changed

Lines changed: 1362 additions & 54 deletions

_quarto.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,45 @@ website:
4040
- text: '   1.1. Prerequisite confirmation'
4141
href: modules/module0/module0-02-are_you_ready.qmd
4242
- section: "**M1. Python & Pandas - An Unexpected Friendship**"
43+
contents:
44+
- href: modules/module1/module1-00-module_learning_outcomes.qmd
45+
- href: modules/module1/module1-01-introduction_to_dataframes.qmd
46+
- text: '   1.1. Exercises'
47+
href: modules/module1/module1-02-describing_a_dataframe.qmd
48+
- href: modules/module1/module1-03-introduction_to_pandas.qmd
49+
- text: '   2.1. Exercises'
50+
href: modules/module1/module1-04-definitions.qmd
51+
- href: modules/module1/module1-07-slicing_with_pandas_using_.loc[].qmd
52+
- text: '   3.1. Exercises'
53+
href: modules/module1/module1-08-slicing_and_dicing_practice.qmd
54+
- href: modules/module1/module1-11-slicing_columns_using_.loc[].qmd
55+
- text: '   4.1. Exercises'
56+
href: modules/module1/module1-12-slicing_columns_only.qmd
57+
- href: modules/module1/module1-13-selecting_using_.loc[].qmd
58+
- text: '   5.1. Exercises'
59+
href: modules/module1/module1-14-rearraging_columns_and_rows.qmd
60+
- href: modules/module1/module1-16-obtaining_dataframe_values.qmd
61+
- text: '   6.1. Exercises'
62+
href: modules/module1/module1-17-practicing_selecting_values.qmd
63+
- href: modules/module1/module1-18-selecting_a_single_column.qmd
64+
- text: '   7.1. Exercises'
65+
href: modules/module1/module1-19-practicing_selecting.qmd
66+
- href: modules/module1/module1-20-slicing_and_selecting_using_df.iloc[].qmd
67+
- text: '   8.1. Exercises'
68+
href: modules/module1/module1-21-practicing_slicing_and_selecting_using_index_position.qmd
69+
- href: modules/module1/module1-24-sorting_dataframes.qmd
70+
- text: '   9.1. Exercises'
71+
href: modules/module1/module1-25-practice_sorting.qmd
72+
- href: modules/module1/module1-26-summary_statistics.qmd
73+
- text: '   10.1. Exercises'
74+
href: modules/module1/module1-27-fruit_bowl_statistics.qmd
75+
- href: modules/module1/module1-29-frequency_tables_and_writing_csvs.qmd
76+
- text: '   11.1. Exercises'
77+
href: modules/module1/module1-30-practicing_frequency_tables.qmd
78+
- href: modules/module1/module1-31-quick_viz_with_altair.qmd
79+
- text: '   12.1. Exercises'
80+
href: modules/module1/module1-32-practicing_bar_charts.qmd
81+
- href: modules/module1/module1-34-what_did_we_just_learn.qmd
4382

4483
# Since we are declaring options for two formats here (html and revealjs)
4584
# each qmd file needs to include a yaml block including which format to use for that file.

modules/module1/module1-03-introduction_to_pandas.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ format:
1212

1313
<iframe
1414
class="video"
15-
src="https://www.youtube.com/embed/W88f5DAl9hk?start=120&end=371&rel=0"
15+
src="https://www.youtube.com/embed/W88f5DAl9hk?start=121&end=371&rel=0"
1616
title="Module 1 Video - Introduction to Pandas"
1717
frameborder="0"
1818
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"

modules/module1/module1-04-definitions.qmd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ hockey_players = pd.read_csv('data/canucks.csv')
126126
hockey_players
127127
```
128128

129-
<br>
130-
131129
What are the column names of the hockey_players dataframe?
132130

133131
**Tasks:**

modules/module1/module1-08-slicing_and_dicing_practice.qmd

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,21 @@ Running a coding exercise for the first time, could take a bit of time for every
6363

6464
**When you see `____` in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.**
6565

66-
### Practicing Rows Slicing
67-
6866
This following code chunk will be used to give you an output of the data `hockey_players`. This will help answer and code in the exercises following it.
6967

7068
```{pyodide}
7169
import pandas as pd
70+
pd.set_option('display.max_columns', 10)
71+
pd.set_option('display.max_rows', 25)
7272
7373
# The data
7474
hockey_players = pd.read_csv('data/canucks.csv')
7575
hockey_players
7676
```
7777

78+
79+
### Practicing Rows Slicing
80+
7881
Using our `hockey_players` data from the last few questions, let's try slicing it.
7982

8083
**Tasks:**
@@ -142,16 +145,6 @@ benched_players
142145

143146
### Practicing Column Slicing
144147

145-
Use the output of the following code chunk to help answer the next question.
146-
147-
```{pyodide}
148-
import pandas as pd
149-
150-
# The data
151-
hockey_players = pd.read_csv('data/canucks.csv')
152-
hockey_players
153-
```
154-
155148
Using our `hockey_players` data from the last few questions, let's try slicing it.
156149

157150
**Tasks:**

modules/module1/module1-11-slicing_columns_using_.loc[].qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ format:
1212

1313
<iframe
1414
class="video"
15-
src="https://www.youtube.com/embed/W88f5DAl9hk?start=651&end=758&rel=0"
15+
src="https://www.youtube.com/embed/W88f5DAl9hk?start=652&end=758&rel=0"
1616
title="Module 1 Video - Slicing Columns Using .loc[]"
1717
frameborder="0"
1818
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
format: html
3+
---
4+
5+
<script src='../../src/quiz.js'></script>
6+
7+
# 4.1. Exercises
8+
9+
## Slicing Columns Only
10+
11+
12+
Using my dataframe object named `fruit_salad`, let's answer some slicing questions.
13+
14+
```out
15+
name colour location seed shape sweetness water-content weight
16+
0 apple red canada True round True 84 100
17+
1 banana yellow mexico False long True 75 120
18+
2 cantaloupe orange spain True round True 90 1360
19+
3 dragon-fruit magenta china True round False 96 600
20+
4 elderberry purple austria False round True 80 5
21+
5 fig purple turkey False oval False 78 40
22+
6 guava green mexico True oval True 83 450
23+
7 huckleberry blue canada True round True 73 5
24+
8 kiwi brown china True round True 80 76
25+
9 lemon yellow mexico False oval False 83 65
26+
```
27+
28+
<div id='mcq1'></div>
29+
<script>
30+
generateQuiz(
31+
'mcq1',
32+
'Question 1',
33+
'If you wanted all the rows and only columns <code>seed</code>, <code>shape</code>, <code>sweetness</code> and <code>water-content</code>, what would your code look like using index labels?',
34+
{
35+
'<code>fruit_salad.loc[:, "seed":"weight"]</code>': 'This is almost right but it may be good to check your columns.',
36+
'<code>fruit_salad[:, "seed":"water-content"]</code>': 'This is almost right but it’s missing something important! Try looking at the title of the slides we just finished.',
37+
'<code>fruit_salad[0:9, "seed":"water-content"]</code>': 'This is almost right but it’s missing something important and it could be condensed! Try looking at the title of the slides we just finished.',
38+
'<code>fruit_salad.loc[:, "seed":"water-content"]</code>': 'Good job! This has both <code>.loc[]</code> and includes the columns we wish to slice here.',
39+
},
40+
'<code>fruit_salad.loc[:, "seed":"water-content"]</code>',
41+
);
42+
</script>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
format: live-html
3+
---
4+
5+
<script src='../../src/quiz.js'></script>
6+
7+
# 5.1. Exercises
8+
9+
## Rearranging Columns and Rows
10+
11+
Using my `fruit_salad` dataframe from earlier...
12+
13+
```out
14+
name colour location seed shape sweetness water-content weight
15+
0 apple red canada True round True 84 100
16+
1 banana yellow mexico False long True 75 120
17+
2 cantaloupe orange spain True round True 90 1360
18+
3 dragon-fruit magenta china True round False 96 600
19+
4 elderberry purple austria False round True 80 5
20+
5 fig purple turkey False oval False 78 40
21+
6 guava green mexico True oval True 83 450
22+
7 huckleberry blue canada True round True 73 5
23+
8 kiwi brown china True round True 80 76
24+
9 lemon yellow mexico False oval False 83 65
25+
```
26+
27+
<div id='mcq1'></div>
28+
<script>
29+
generateQuiz(
30+
'mcq1',
31+
'Question 1',
32+
'If I wanted to make a tropical salad and the recipe calls for <code>kiwi</code>, <code>cantaloupe</code> and <code>guava</code> in this order and I am only interested in columns ordered as <code>sweetness</code>, <code>weight</code>, <code>seed</code> and <code>location</code>, what would my code look like?',
33+
{
34+
'<code>fruit_salad.loc[8, 2, 6:"sweetness", "weight", "seed", "location"]</code>': 'Unfortunately, this code has many errors in it.',
35+
'<code>fruit_salad.loc[[8, 2, 6]:["sweetness", "weight", "seed", "location"]]</code>': 'The way that the rows and columns are separated may need to be looked over.',
36+
'<code>fruit_salad.loc[[8, 2, 6], ["sweetness", "weight", "seed", "location"]]</code>': 'Yes, this looks right!',
37+
'<code>fruit_salad.loc[[2, 6, 8], ["location", "seed”, “sweetness", "weight"]]</code>': 'Remember we are rearranging here.',
38+
},
39+
'<code>fruit_salad.loc[[8, 2, 6], ["sweetness", "weight", "seed", "location"]]</code>',
40+
);
41+
</script>
42+
43+
## Practicing Selecting Using Index Labels
44+
45+
**Instructions:**
46+
Running a coding exercise for the first time, could take a bit of time for everything to load. Be patient, it could take a few minutes.
47+
48+
**When you see `____` in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.**
49+
50+
Use the output of the following code chunk to help answer the next question.
51+
52+
```{pyodide}
53+
import pandas as pd
54+
pd.set_option('display.max_columns', 10)
55+
pd.set_option('display.max_rows', 25)
56+
57+
# The data
58+
hockey_players = pd.read_csv('data/canucks.csv')
59+
hockey_players
60+
```
61+
62+
Let's select specific players and columns.
63+
64+
**Tasks:**
65+
66+
- Select the players `Zack MacEwan`, `Jake Virtanen` and `Jordie Benn` in that order and the columns `Height`, `Weight`, `Salary` and `Country` in that order.
67+
- Save the new sliced dataframe as object `penalty_players`.
68+
- Display it.
69+
70+
```{pyodide}
71+
#| setup: true
72+
#| exercise: practicing_selecting_using_index_labels
73+
import pandas as pd
74+
from src.utils import print_correct_msg
75+
76+
hockey_players = pd.read_csv('data/canucks.csv')
77+
```
78+
79+
```{pyodide}
80+
#| exercise: practicing_selecting_using_index_labels
81+
# Select the rows and columns
82+
# Save the new dataframe as penalty_players
83+
____ = ____.____[____, ____]
84+
85+
# Display it
86+
____
87+
```
88+
89+
```{pyodide}
90+
#| exercise: practicing_selecting_using_index_labels
91+
#| check: true
92+
solution = hockey_players.loc[[10, 21, 2], ['Height', 'Weight', 'Salary', 'Country']]
93+
assert isinstance(result, pd.DataFrame), "The result should be a dataframe."
94+
assert solution.shape == result.shape, "You may not have selected correctly."
95+
assert list(solution.columns) == list(result.columns), "Your columns do not seem to be correct."
96+
assert list(solution.index) == list(result.index), "Your rows do not seem to be correct."
97+
print_correct_msg()
98+
```
99+
100+
:::: { .hint exercise="practicing_selecting_using_index_labels" }
101+
::: { .callout-note collapse="false" }
102+
103+
## Hint 1
104+
105+
- Are you using `.loc[]`?
106+
- Are you using the correct dataframe labels? Are you using names as your row labels?
107+
- Did you slice both columns and rows?
108+
- Are you using 2 sets of `[]` brackets?
109+
- Are you using "quotations"?
110+
111+
:::
112+
::::
113+
114+
:::: { .solution exercise="practicing_selecting_using_index_labels" }
115+
::: { .callout-tip collapse="false" }
116+
117+
## Fully worked solution:
118+
119+
```{pyodide}
120+
# Select the rows and columns
121+
# Save the new dataframe as penalty_players
122+
penalty_players = hockey_players.loc[[10, 21, 2], ['Height', 'Weight', 'Salary', 'Country']]
123+
124+
# Display it
125+
penalty_players
126+
```
127+
128+
:::
129+
::::

modules/module1/module1-16-obtaining_dataframe_values.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ format:
1212

1313
<iframe
1414
class="video"
15-
src="https://www.youtube.com/embed/W88f5DAl9hk?start=830&end=907&rel=0"
15+
src="https://www.youtube.com/embed/W88f5DAl9hk?start=831&end=907&rel=0"
1616
title="Module 1 Video - Obtaining Dataframe Values"
1717
frameborder="0"
1818
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"

0 commit comments

Comments
 (0)