|
| 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 | +:::: |
0 commit comments