Your code is missing the `.agg()` step — it’s grouping but not summing the amounts. How would you fix this?
💡 Model Answer
The issue is that after calling groupBy("customer_id") you need to specify an aggregation function to compute a value for each group. Without .agg(), the DataFrame simply contains the grouping keys and no aggregated metrics. The fix is to add an aggregation, such as summing the amount column. A corrected snippet would be:
from pyspark.sql.functions import sum as _sum
result = (
df.groupBy("customer_id")
.agg(_sum("amount").alias("total_amount"))
)This groups the rows by customer_id and then sums the amount column for each group, producing a DataFrame with columns customer_id and total_amount. If you need additional aggregates, you can chain them inside the agg() call. The missing .agg() was the root cause of the error.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500