CompTIA Data+ (DA0-002) Practice Questions

CompTIA Data+ exam preparation

CompTIA Data+ (DA0-002) practice questions in English: 180 questions with explanations. The mock exam has 90 questions and 90 minutes.

CompTIA Data+ (DA0-002) free question 6 / 10 · Domain 3: Data analysis

A pandas script contains the statement if df['amount'] > 100: and fails with ValueError: The truth value of a Series is ambiguous. How should the analyst fix it?

  1. Convert the amount column to a different data type, as the error is caused by a mismatch of types
  2. Replace the comparison with the Python keyword and, which joins the values of the Series into one result
  3. Wrap the comparison in bool() to convert the whole Series into True or False
  4. Decide what the test means, then reduce the Series to one value, such as whether at least one entry is true
Show the answer and explanation

Answer: D

Why this is correct
The pandas FAQ explains that pandas follows the NumPy convention of raising an error when you try to convert something to a bool. This happens in an if statement or with the boolean operations and, or and not. The user has to choose explicitly what is meant, for example with any(), all() or empty, and that is what removes the ambiguity.

Why the other options are wrong
"Wrap the comparison in bool() to convert the whole Series into True or False" … Converting a Series to a bool is exactly what raises the error. pandas refuses because it is unclear whether a non-empty Series should count as true.
"Replace the comparison with the Python keyword and, which joins the values of the Series into one result" … The boolean operators and, or and not also try to convert the Series to a bool, and they raise the same error.
"Convert the amount column to a different data type, as the error is caused by a mismatch of types" … The message is not about types. A comparison already returns a Boolean Series, and the problem is using that Series as a single condition.

Where people trip
A comparison on a column gives a result per row. Reduce it with any() or all(), or use it to select rows, before putting it in an if.

Go to the CompTIA Data+ (DA0-002) practice app (10 free questions; purchase unlocks all questions)

See the full practice set