Write a pandas script that answers specific questions about a dataset, with a cleaning log, validated merges, grouped results, pivots, rankings and charts tied to each question.
Write a Python script that uses pandas to answer the analysis questions below. Every transformation must be traceable: record how many rows each cleaning step removed or changed, validate merges, and connect each table or chart to the question it answers. ## Inputs Dataset file(s), format and column descriptions: [DATASET] Questions the analysis must answer: [QUESTIONS] Second dataset to merge and its join key, if any: [LOOKUP_DATA] Known data issues (missing values, duplicates, mixed types, date formats): [KNOWN_ISSUES] Python and pandas versions; charting library (matplotlib or seaborn): [ENVIRONMENT] Output destination (console, CSV files, notebook, images): [OUTPUT] ## Instructions 1. Load the CSV with explicit dtypes and date parsing where known. 2. Clean data and log each step with row counts before and after: handle missing values with a rule per column (drop, fill, or keep as missing), remove exact duplicates, convert types and report values that failed conversion. 3. Filter only as the questions require, and log rows affected. 4. Group and aggregate (for example sum, mean, median, count) to answer each question. 5. Merge with [LOOKUP_DATA] using `validate=` (for example `many_to_one`) and `indicator=True`, and report unmatched rows instead of dropping them silently. 6. Pivot from long to wide format where a question compares categories across periods. 7. Sort and rank results; state the tie-handling method. 8. Create charts only where they answer a question; label axes and units and save them to [OUTPUT]. 9. Keep the code reproducible: functions per step, no hidden notebook state, a `main()` entry point. ## Return 1. Short description of the dataset and the analysis. 2. The script. 3. A cleaning log table: step, rows before, rows after, notes. 4. For each question: the table or chart that answers it and how to read it. 5. Limitations: assumptions, excluded rows and what the data cannot show (for example causation). ## Acceptance checks - Row counts reconcile from load to final tables. - Every merge uses `validate` and reports unmatched keys. - Every chart maps to a question. - No results are stated as findings until the script has run on the real data.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Dataset: sales.csv with order_id, order_date (YYYY-MM-DD), region_code, product_id, quantity, unit_price Questions: 1) total revenue by region per month in 2025; 2) top 5 products by revenue in each region; 3) share of orders with missing region_code Lookup: regions.csv with region_code (unique), region_name Known issues: duplicate rows from a re-run export; some unit_price values contain a currency symbol Environment: Python 3.12, pandas 2.3, matplotlib Output: CSV tables and PNG charts in ./report
A script that loads sales.csv with parse_dates for order_date, strips currency symbols before converting unit_price and reports rows that still fail, drops exact duplicates with a logged count, computes revenue, merges regions with validate="many_to_one" and indicator=True, reports orders with missing or unmatched region_code (question 3) rather than dropping them, builds a region-by-month pivot for 2025 (question 1), ranks products within each region with an explicit tie method (question 2), saves one line chart for question 1, and writes a cleaning log. Illustrative expectation; merge validation and the cleaning-log reconciliation were executed locally with pandas on a synthetic fixture (see report).
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: