pivot_table für Kreuztabellen
Über zwei Dimensionen hinweg zusammenfassen
pivot_table für Kreuztabellen ist eine kostenlose Data Science Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.
From Long Rows to a Grid
When you want a category-by-category summary, pivot_table turns tidy long rows into a clean two-dimensional grid. 🔄
The Three Key Arguments
Every pivot table needs three roles: which column becomes index, which becomes columns, and which values fill the cells.
df.pivot_table(index='region',
columns='month',
values='sales')It Aggregates by Default
If several rows land in the same cell, pivot_table averages them. The default aggfunc is mean, not a plain copy.
Choose Your Aggregation
Swap the math with aggfunc. Use sum for totals, count for tallies, or max to spot the peak value per cell.
df.pivot_table(index='region',
values='sales',
aggfunc='sum')Many Aggregations at Once
Pass a list to aggfunc and get several summaries together. One call can return both the mean and the count side by side.
df.pivot_table(values='sales',
aggfunc=['mean', 'count'])Filling Empty Cells
Missing combinations show up as NaN. Set fill_value to replace those gaps with zero or any sensible default.
df.pivot_table(index='region',
columns='month',
fill_value=0)Add Row and Column Totals
Set margins to True and pivot_table adds an All row and column with grand totals around the edges.
df.pivot_table(index='region',
margins=True)Group by Several Keys
Pass a list to index or columns to nest categories. The result gains a MultiIndex that stacks the groups neatly.
df.pivot_table(index=['region', 'store'],
values='sales')pivot vs pivot_table
Plain pivot needs unique index-column pairs and cannot aggregate. Reach for pivot_table whenever duplicates might appear.
Reading a Cross-Tab
In the result, scan a cell by its row label and column label to read that exact category combination at a glance.
Back to a Flat Table
A pivot result is still a DataFrame. Call reset_index to flatten its labels back into ordinary columns for export.
summary.reset_index()Quick Check
Let us check your pivot_table reflexes.
Recap: Cross-Tabs Made Easy
You built grids with index, columns, values, and aggfunc. Next you will reverse course and melt wide data back to long. 🎯
Häufig gestellte Fragen
Ist die Lektion „pivot_table für Kreuztabellen“ kostenlos?
Ja — der vollständige Text von „pivot_table für Kreuztabellen“ 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 „pivot_table für Kreuztabellen“?
Über zwei Dimensionen hinweg zusammenfassen 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 2 von 4.
Wie lange dauert die Lektion „pivot_table für Kreuztabellen“?
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