Practice / aggregations

Count the Refunds

medium

The Daily Grind's finance team wants a refund tally per store — and crucially, a store that issued no refunds should still show up with a 0, not vanish from the report.

You have the txns DataFrame:

Your task: return each store and a column refunds — the number of transactions whose status is "refund". Every store must appear, including those with zero refunds. Use conditional aggregation rather than pre-filtering.

Assign your answer DataFrame to result. Row order doesn't matter.

txnsinput DataFrame

Schema
columntype
txn_idlong
storestring
statusstring
Sample rows
txn_idstorestatus
1Downtownsale
2Downtownrefund
3Downtownsale
4Pierrefund
5Pierrefund
6Airportsale
7Airportsale
Expected output shape
storerefunds· 3 rows
Hint

Use conditional aggregation, not a pre-filter: F.sum(F.when(F.col("status") == "refund", 1).otherwise(0)).alias("refunds"). The .otherwise(0) is what keeps zero-refund stores in the result.

Lesson refresher

This problem builds on Aggregations & groupBy (~7 min). Pop it open in a new tab if you want a quick recap.

Loading editor…
Hit Run to execute your code and see the output here.