Python for data practice exam
Fifteen questions across pandas, cleaning, aggregation and plotting.
Which reads every sheet of a workbook into a dictionary?
- sheet_name=0
- sheet_name=None
- sheet_name="all"
- sheets=True
Answer: sheet_name=None — None returns a dict keyed by sheet name.
info() reports a numeric column as float64 unexpectedly. Likely cause?
- Large values
- The column contains NaN
- Wrong encoding
- It is an index
Answer: The column contains NaN — A single missing value forces an integer column to float.
df[df['a'] > 1 and df['b'] < 5] fails because:
- a is text
- pandas requires & rather than and
- Missing .loc
- Brackets are wrong
Answer: pandas requires & rather than and — The "truth value is ambiguous" error.
Which keeps every original row while adding a group total?
- agg
- transform
- filter
- apply
Answer: transform — transform aligns the group result back onto each row.
A left merge increased the row count. This means:
- Correct behaviour
- Duplicate keys on the right side
- Missing values
- Wrong suffixes
Answer: Duplicate keys on the right side — The right table has more than one row per key.
Which argument raises if the merge relationship is not as expected?
- indicator
- validate
- how
- on
Answer: validate — validate='many_to_one' fails fast.
Which turns month columns into month rows?
- pivot_table
- melt
- concat
- stack
Answer: melt — melt is the unpivot operation.
errors="coerce" in to_numeric does what to bad values?
- Raises
- Converts them to NaN
- Leaves as text
- Drops the row
Answer: Converts them to NaN — Letting the rest of the column convert.
Why pass format= to pd.to_datetime?
- Speed only
- To avoid ambiguous day/month misparsing
- It is mandatory
- To handle nulls
Answer: To avoid ambiguous day/month misparsing — 03/04/2024 is ambiguous and may be guessed inconsistently.
SettingWithCopyWarning is fixed by:
- Ignoring it
- Adding .copy() after filtering, or using df.loc
- Using apply
- reset_index
Answer: Adding .copy() after filtering, or using df.loc — pandas cannot tell whether you hold a view or a copy.
Which handles multiple ordered conditions?
- np.where
- np.select
- astype
- fillna
Answer: np.select — np.select takes condition and choice lists.
To keep the latest record per email:
- drop_duplicates() alone
- sort_values then drop_duplicates(keep="last")
- dropna()
- groupby
Answer: sort_values then drop_duplicates(keep="last") — Without sorting, which row survives is arbitrary.
Why must a bar chart start at zero?
- Convention
- Bar length encodes value, so truncation misleads
- matplotlib requires it
- For the legend
Answer: Bar length encodes value, so truncation misleads — Truncation exaggerates small differences.
Why does seaborn need long-form data for hue=?
- Performance
- The grouping variable must be a single column
- It cannot read wide data
- For colour maps
Answer: The grouping variable must be a single column — Which is why melt matters before plotting.
to_csv without index=False produces:
- A smaller file
- An unnamed extra index column
- An error
- Missing headers
Answer: An unnamed extra index column — And it compounds on every round trip.