Mit melt ins Long-Format wechseln
Spalten in Zeilen umformen
Mit melt ins Long-Format wechseln ist eine kostenlose Data Science Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Data Science Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Data Science Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Mit melt ins Long-Format wechseln“ kostenlos?
Ja — der vollständige Text von „Mit melt ins Long-Format wechseln“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Data Science Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Data Science Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Mit melt ins Long-Format wechseln“?
Spalten in Zeilen umformen Du übst Data Science Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Data Science Academy zu starten?
Keine Vorkenntnisse erforderlich. Data Science Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Mit melt ins Long-Format wechseln“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Data Science Academy-Lektion Code schreiben und ausführen?
Ja. Jede Data Science Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Wide und Long sowie aufgeräumte Daten
- pivot_table für Kreuztabellen
- Mit melt ins Long-Format wechseln
- stack, unstack und MultiIndex