pbPassingBI
/
8 questions

Aggregation & reshaping — quiz

8 questions covering this module. groupby, sorting, merging and pivoting.

  1. Which keeps the original row count?

    1. agg
    2. transform
    3. sum
    4. count

    Answer: transform — transform aligns the result back onto every row, like a window function.

  2. Why call reset_index() after groupby?

    1. To sort
    2. To turn the grouping key back into a normal column
    3. To remove nulls
    4. To save memory

    Answer: To turn the grouping key back into a normal column — Otherwise it stays in the index and complicates plotting and merging.

  3. df['x'].rank() on tied values gives, by default:

    1. Sequential integers
    2. The average rank, e.g. 2.5
    3. The lowest rank
    4. An error

    Answer: The average rank, e.g. 2.5 — method="min" or "dense" match SQL behaviour.

  4. The efficient way to get the top 10 rows by revenue:

    1. sort_values then head
    2. nlargest(10, "revenue")
    3. rank then filter
    4. groupby

    Answer: nlargest(10, "revenue") — nlargest avoids sorting the whole frame.

  5. A left merge increases the row count. This means:

    1. Normal behaviour
    2. The right table has duplicate keys
    3. Missing values
    4. Wrong how=

    Answer: The right table has duplicate keys — Duplicate keys on the right fan the rows out.

  6. Which argument raises an error if the relationship is not as expected?

    1. indicator
    2. validate
    3. suffixes
    4. how

    Answer: validate — validate='many_to_one' checks the key relationship.

  7. Which converts month columns into rows?

    1. pivot_table
    2. melt
    3. stack
    4. concat

    Answer: melt — melt unpivots wide data into long form.

  8. pivot_table differs from pivot because it:

    1. Is faster
    2. Aggregates duplicate entries rather than raising
    3. Only takes one column
    4. Requires a date index

    Answer: Aggregates duplicate entries rather than raising — pivot raises when the index/column pair is not unique.