Spring 2024 Quiz 2

← return to practice.dsc10.com


This quiz was administered in-person. It was closed-book and closed-note; students were not allowed to use the DSC 10 Reference Sheet. Students had 20 minutes to work on the quiz.

This quiz covered Lectures 5-9 of the Spring 2024 offering of DSC 10.

⚠️ PDF version available here .


That's the end of the quiz.

An art museum records information about its collection in a DataFrame called art. The columns of art are as follows:


Problem 1

Lecture 5

Write an expression that evaluates to the number of art pieces made in 1950 that cost less than $10,000.

Answer: art[(art.get("year") == 1950) & (art.get("price") < 10000)].shape[0]

We want rows where both conditions are true, so we join them with &. Each condition needs its own parentheses. Filtering art keeps only those rows, and .shape[0] counts them.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 72%.


Problem 2

Lecture 9


Problem 2.1

Fill in the blanks in the code below to find the name of the artist in art who has the lowest mean price of art pieces.

art.groupby(___(x)___).___(y)___.sort_values(by="price").index[0]

Answer: (x): "artist", (y): mean()

Grouping by "artist" gives one row per artist. .mean() finds each artist’s average price. Sorting by "price" puts the lowest average first. After a groupby, the index is the artist names, so .index[0] is the artist with the lowest mean price.

Note: In newer versions of babypandas, .mean() raises an error if the DataFrame has a column that isn’t numeric, like "title". To run this code today, first keep only the columns you need: art.get(["artist", "price"]).groupby("artist").mean().


Difficulty: ⭐️

The average score on this problem was 94%.


Problem 2.2

Fill in the blanks in the code below to find the name of the artist in art who made the most art pieces in a single year.

(art.groupby(___(x)___).___(y)___.reset_index()
        .sort_values(by="title", ascending=False)
        .get("artist").iloc[0])

Answer: (x): ["artist", "year"] or ["year", "artist"], (y): count()

We need the number of pieces each artist made in each year, so we group by both columns. That gives one row for each artist and year. .count() counts the pieces in each group, so every column, including "title", holds that count. Sorting by "title" in descending order puts the biggest count first. .reset_index() turns "artist" back into a column, so .get("artist") works. If we grouped by "artist" alone, we would count each artist’s pieces across all years, not in a single year.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 59%.



Problem 3

Lecture 7


Problem 3.1

Which of the following correctly plots a density histogram showing the distribution of "price" in art? Select all that apply.

Answer: Options 1 and 5

A density histogram needs density=True, so Options 2 and 4 are wrong. Option 1 keeps only the "price" column and plots it. Option 5 picks "price" with y="price". Option 3 drops "price", so only "title" is left, and titles are strings. Option 6 uses x="price" instead of y="price". For a histogram, this leaves "price" out and plots "year" instead.


Difficulty: ⭐️⭐️

The average score on this problem was 83%.


Problem 3.2

The density histogram below shows the distribution of "price" in art. If the museum has 100 art pieces in total, how many pieces cost at least $3,000 but less than $4,500?

Answer: 30

In a density histogram, the area of a bar is the proportion of values in that bin. Area is height times width, and each bar is 500 dollars wide.

  • 3000 to 3500: 0.00025 * 500 = 0.125
  • 3500 to 4000: 0.00015 * 500 = 0.075
  • 4000 to 4500: 0.0002 * 500 = 0.1

Together, that is 0.3 of the pieces. With 100 pieces, that is 0.3 * 100 = 30.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 55%.



Problem 4

Lecture 9


Problem 4.1

Fill in the return statement of the function is_expensive, which takes as input the price of an art piece (as a float, in dollars) and returns True if the price is more than 20 million dollars. Otherwise, it returns False.

def is_expensive(price):
        return ___(a)___

Answer: price > 20_000_000

A comparison like price > 20_000_000 is already True or False, so we can return it directly. The underscores just make the number easier to read. 20000000 works too.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 74%.



Problem 4.2

Write one line of code to add a new column called exp to the art DataFrame, which categorizes if each art piece is worth more than 20 million dollars, using Boolean values. You must use the is_expensive function you wrote above. Make sure to modify art!

Answer: art = art.assign(exp = art.get("price").apply(is_expensive))

.apply(is_expensive) calls the function on every price and gives back a Series of True/False values. .assign adds that Series as a new column called exp. .assign doesn’t change art by itself, so we save the result back into art.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 73%.


Problem 4.3

Next, we make a new DataFrame called expensive as follows.

expensive = art[art.get("exp")]
merged = art.merge(expensive, on="artist")

Van Gogh is one artist represented in art and exactly half of his pieces in art are worth over 20 million dollars. If Van Gogh’s art appears in 72 rows of the merged DataFrame, how many rows does Van Gogh actually have in the original art DataFrame?

Answer: 12

Say Van Gogh has n pieces in art. Half are expensive, so he has n / 2 pieces in expensive. When we merge on "artist", each of his n rows in art pairs with each of his n / 2 rows in expensive. That gives n * n / 2 rows. Setting n * n / 2 = 72 gives n * n = 144, so n = 12.


Difficulty: ⭐️⭐️⭐️⭐️⭐️

The average score on this problem was 16%.



👋 Feedback: Find an error? Still confused? Have a suggestion? Let us know here.