A payments export arrives as text: customer ids with stray spaces and placeholder values, timestamps in three different layouts, and amounts with currency symbols. Clean it and report each customer's transaction count and total.
Write clean_transactions(df). The input df is a pandas DataFrame with string (or missing) columns customer_id, ts and amount. Return a new DataFrame with columns customer_id, n_txns, total_amount, one row per customer, sorted by customer_id, with a fresh 0-based index. Do not modify df.
Cleaning rules:
customer_id: strip surrounding whitespace and upper-case it. It is invalid if missing, empty after stripping, or one of NULL, NONE, NAN (any case).
ts: strip whitespace, then accept exactly one of these layouts, where month, day and hour may have one or two digits:
YYYY-MM-DD HH:MMYYYY/MM/DD HH:MMDD-MM-YYYY HH:MMAnything else, or a date or time that does not exist (such as 30 February), is invalid.
amount: strip whitespace, remove one leading $ and any , thousands separators, then parse as a number. It is invalid if missing, unparseable, or not strictly positive.
Processing order: drop every row with any invalid field, then remove duplicates, where two rows are duplicates if they have the same cleaned customer_id and the same parsed timestamp (keep the first in the original order). Finally group by customer: n_txns is the row count and total_amount is the sum of amounts rounded to 2 decimals.