melt to Go Long
Unpivoting columns into rows.
melt to Go Long is a free Data Science Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Opposite of Pivot
When columns are really values in disguise, melt unpivots them into rows, turning a wide table into tidy long form. 🫠
What melt Produces
Melt creates two new columns: one holding the old column names, one holding their values. Every cell becomes its own row.
pd.melt(df)Keep Some Columns Fixed
Use id_vars to list columns that should stay put, like an id or name. Those repeat down the new long table.
df.melt(id_vars='name')Choose What to Unpivot
Set value_vars to melt only specific columns. Anything left out and not in id_vars is simply dropped from the result.
df.melt(id_vars='name',
value_vars=['Jan', 'Feb'])Name Your New Columns
The defaults are variable and value. Pass var_name and value_name to give them meaningful labels like month and sales.
df.melt(var_name='month',
value_name='sales')Why Melt Before Plotting
Libraries like seaborn want long input. A quick melt lets you map one column to color or facet and plot many series at once.
Melt Then GroupBy
Once data is long, summaries get simple. After melt you can groupby the new variable column and aggregate every series uniformly.
df.melt(id_vars='name').groupby('variable')Melt and Pivot Are Inverses
Melt and pivot undo each other. Go wide to long with melt, then pivot back, and you recover the original layout.
Watch the Row Explosion
Melting multiplies rows: ten columns over five rows becomes fifty long rows. Expect the row count to grow fast.
wide_to_long for Patterns
For columns sharing a prefix like sales_2021, wide_to_long melts them while parsing the year out of each name.
A Clean Melt Recipe
A reliable pattern: name your id columns, name the value columns, and label the output. That gives a fully tidy long table.
df.melt(id_vars='name',
var_name='month',
value_name='sales')Quick Check
Quick check on melting wide tables.
Recap: Going Long
You used melt with id_vars, value_vars, and custom names to tidy wide data. Next: stack and unstack on a MultiIndex. 🎯
Frequently asked questions
Is the “melt to Go Long” lesson free?
Yes — the full text of “melt to Go Long” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “melt to Go Long”?
Unpivoting columns into rows. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “melt to Go Long” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.