pbPassingBI
/

Python for data practice exam

Fifteen questions across pandas, cleaning, aggregation and plotting.

  1. Which reads every sheet of a workbook into a dictionary?

    1. sheet_name=0
    2. sheet_name=None
    3. sheet_name="all"
    4. sheets=True

    Answer: sheet_name=None — None returns a dict keyed by sheet name.

  2. info() reports a numeric column as float64 unexpectedly. Likely cause?

    1. Large values
    2. The column contains NaN
    3. Wrong encoding
    4. It is an index

    Answer: The column contains NaN — A single missing value forces an integer column to float.

  3. df[df['a'] > 1 and df['b'] < 5] fails because:

    1. a is text
    2. pandas requires & rather than and
    3. Missing .loc
    4. Brackets are wrong

    Answer: pandas requires & rather than and — The "truth value is ambiguous" error.

  4. Which keeps every original row while adding a group total?

    1. agg
    2. transform
    3. filter
    4. apply

    Answer: transform — transform aligns the group result back onto each row.

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

    1. Correct behaviour
    2. Duplicate keys on the right side
    3. Missing values
    4. Wrong suffixes

    Answer: Duplicate keys on the right side — The right table has more than one row per key.

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

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

    Answer: validate — validate='many_to_one' fails fast.

  7. Which turns month columns into month rows?

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

    Answer: melt — melt is the unpivot operation.

  8. errors="coerce" in to_numeric does what to bad values?

    1. Raises
    2. Converts them to NaN
    3. Leaves as text
    4. Drops the row

    Answer: Converts them to NaN — Letting the rest of the column convert.

  9. Why pass format= to pd.to_datetime?

    1. Speed only
    2. To avoid ambiguous day/month misparsing
    3. It is mandatory
    4. To handle nulls

    Answer: To avoid ambiguous day/month misparsing — 03/04/2024 is ambiguous and may be guessed inconsistently.

  10. SettingWithCopyWarning is fixed by:

    1. Ignoring it
    2. Adding .copy() after filtering, or using df.loc
    3. Using apply
    4. reset_index

    Answer: Adding .copy() after filtering, or using df.loc — pandas cannot tell whether you hold a view or a copy.

  11. Which handles multiple ordered conditions?

    1. np.where
    2. np.select
    3. astype
    4. fillna

    Answer: np.select — np.select takes condition and choice lists.

  12. To keep the latest record per email:

    1. drop_duplicates() alone
    2. sort_values then drop_duplicates(keep="last")
    3. dropna()
    4. groupby

    Answer: sort_values then drop_duplicates(keep="last") — Without sorting, which row survives is arbitrary.

  13. Why must a bar chart start at zero?

    1. Convention
    2. Bar length encodes value, so truncation misleads
    3. matplotlib requires it
    4. For the legend

    Answer: Bar length encodes value, so truncation misleads — Truncation exaggerates small differences.

  14. Why does seaborn need long-form data for hue=?

    1. Performance
    2. The grouping variable must be a single column
    3. It cannot read wide data
    4. For colour maps

    Answer: The grouping variable must be a single column — Which is why melt matters before plotting.

  15. to_csv without index=False produces:

    1. A smaller file
    2. An unnamed extra index column
    3. An error
    4. Missing headers

    Answer: An unnamed extra index column — And it compounds on every round trip.