pbPassingBI
/
10 questions

Working with DataFrames — quiz

10 questions covering this module. Loading, inspecting, selecting and filtering.

  1. How do you stop 07030 becoming 7030?

    1. astype(int)
    2. dtype={'zip': str} when reading
    3. parse_dates
    4. encoding="utf-8"

    Answer: dtype={'zip': str} when reading — Read identifier-like columns as strings.

  2. pd.read_excel(..., sheet_name=None) returns:

    1. The first sheet
    2. A dictionary of every sheet
    3. An error
    4. An empty DataFrame

    Answer: A dictionary of every sheet — Keyed by sheet name.

  3. info() shows order_date as object. This means:

    1. It is a date
    2. It is stored as text
    3. It contains nulls
    4. It is an index

    Answer: It is stored as text — object means text; convert with to_datetime.

  4. An integer column showing float64 usually indicates:

    1. A bug
    2. Missing values are present
    3. Large numbers
    4. Wrong encoding

    Answer: Missing values are present — NaN forces the column to float.

  5. df[['a']] returns:

    1. A Series
    2. A DataFrame
    3. A list
    4. An index

    Answer: A DataFrame — Double brackets always give a DataFrame.

  6. df.rename(columns={...}) without assignment:

    1. Renames in place
    2. Returns a new DataFrame and changes nothing
    3. Raises an error
    4. Renames the index

    Answer: Returns a new DataFrame and changes nothing — Assign the result, or pass inplace=True.

  7. df[df['a'] > 1 and df['b'] < 5] raises an error because:

    1. a is not numeric
    2. pandas requires & rather than and
    3. Brackets are wrong
    4. It needs .loc

    Answer: pandas requires & rather than and — The "truth value is ambiguous" error means and was used instead of &.

  8. df.loc[0:5] versus df.iloc[0:5]:

    1. Identical
    2. loc includes row 5, iloc excludes it
    3. iloc includes 5, loc excludes it
    4. loc only takes conditions

    Answer: loc includes row 5, iloc excludes it — loc is label-based and slice-inclusive.

  9. Which handles several conditions in order?

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

    Answer: np.select — np.select takes condition and choice lists, first match wins.

  10. SettingWithCopyWarning is best fixed by:

    1. Ignoring it
    2. Adding .copy() after filtering, or assigning via df.loc
    3. Using apply
    4. Resetting the index

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