Reshaping Wide and Long Tables
Pivot, melt, and stack without confusion.
The Open buttons send the prompt text as currently shown below to that site only when you click. Fill in the placeholders first.
Help me reshape a table. Current layout (paste a few rows and the column names): [CURRENT_TABLE]. Target layout I need: [TARGET_LAYOUT]. Tool downstream (chart library, database, spreadsheet): [DOWNSTREAM_TOOL]. Explain and demonstrate: 1. Wide versus long in one paragraph, using my own columns. 2. The exact call (melt, pivot, pivot_table, stack, unstack, or wide_to_long) that turns the current layout into the target, with each argument explained in a comment. 3. A worked example with five invented rows showing input and output side by side as text tables. 4. The failure modes: duplicate index entries that make pivot raise an error (and the pivot_table aggregation that hides them), missing combinations that become NaN, and column names that become a MultiIndex (with the code to flatten it). 5. A round-trip test: reshape to target and back, then assert equality. 6. Which layout suits my downstream tool, with the reason. End with a three-line cheat sheet in my own column names.
Fill in before use
Optional: type your details and the prompt above updates. Nothing is saved or sent.
Replace each bracketed placeholder with your own details, then paste into Any AI or another assistant.
See an example result (fictional data)
Wide table region | Jan | Feb | Mar North | 120 | 130 | 110 South | 90 | 95 | 100 Goal: long format for plotting. Code: long = df.melt(id_vars="region", var_name="month", value_name="sales") Result region | month | sales North | Jan | 120 North | Feb | 130 ... Back to wide: long.pivot(index="region", columns="month", values="sales"). Check: rows long = 2 regions x 3 months = 6. Pitfall: month order becomes alphabetical; set a categorical with the correct order. When to keep wide: for reports where readers compare months side by side.
Illustrative only, made-up data. Your results will differ and must be reviewed.