DA0-002 Data Acquisition and Preparation Practice Question
In pandas, you have a DataFrame 'df' with columns 'product' and 'sales'. You want to calculate the total sales per product. Which method should you use?
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
df.groupby('product')['sales'].sum()
df.groupby('product')['sales'].sum() groups by product and sums sales. df.pivot_table can also do it but is more complex. df.merge is for joining, df.apply is for applying a function element-wise or row/column-wise.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
df['sales'].apply(sum)
Why it's wrong here
Applies sum to each element, not grouping.
- ✗
df.pivot_table(values='sales', index='product', aggfunc='sum')
Why it's wrong here
Although `pivot_table` can aggregate data, it requires a unique index–column combination to reshape the table, whereas the question only needs a simple grouped sum without restructuring. It is tempting because `pivot_table` does accept `aggfunc='sum'`, making it appear suitable for aggregation; it would be correct if the goal were to reorganise the DataFrame into a cross-tabulation with multiple column categories, not merely to compute totals per product.
- ✓
df.groupby('product')['sales'].sum()
Why this is correct
Correctly aggregates sales by product.
- ✗
df.merge(df, on='product')
Why it's wrong here
Merge is for combining DataFrames, not aggregation.
Go deeper
Related to this question
About these practice questions
One of 986 original DA0-002 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This DA0-002 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the DA0-002 exam.