Skip to content

Commit

Permalink
Question 2 explanation, fa24 midterm
Browse files Browse the repository at this point in the history
  • Loading branch information
katefengx committed Nov 18, 2024
1 parent 262fe57 commit 7892b9c
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions problems/fa24-midterm/q02.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ apply.

**Answer**: `treat.get("candy").iloc[1]` and `treat.sort_values(by="candy", ascending = False).get("candy").loc[1]`

- **Option 1**: `treat.get("candy").iloc[1]` gets the `candy` column and then retrieves the value at index location `1`, which would be `"M&M"`.

- **Option 2**: `treat.sort_values(by="candy", ascending=False).get("candy").iloc[1]` sorts the `candy` column in descending order (alphabetically, the last candy is at the top) and then retrieves the value at index location `1` in the `candy` column. The entire dataset is not shown, but in the given rows, the second-to-last candy alphabetically is `"Skittles"`, so we know that `"M&M"` will not be the second-to-last alphabetical candy in the full dataset.

- **Option 3**: `treat.sort_values(by="candy", ascending=False).get("candy").loc[1]` is very similar to the last option; however, this time, `.loc[1]` is used instead of `.iloc[1]`. This means that instead of looking at the row in position `1` (second row) of the sorted DataFrame, we are finding the row with an index label of `1`. When the rows are sorted by `candy` in descending order, the index labels remain with their original rows, so the `"M&M"` row is retrieved when we search for the index label `1`.

- **Option 4**: `treat.set_index("candy").index[-1]` sets the index to the `candy` column and then retrieves the last element in the index (`candy`). The entire dataset is not shown, but in the given rows, the last value would be `"Skittles"` and not `"M&M"`. The last value of the full dataset could be `"M&M"`, but since we are not sure, this option is not selected.

<average>66</average>

# END SOLUTION
Expand Down

0 comments on commit 7892b9c

Please sign in to comment.