stack, unstack i MultiIndex
Zmiana kształtu tabel hierarchicznych
stack, unstack i MultiIndex to bezpłatna lekcja Data Science Academy na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Data Science Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Data Science Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Reshaping With the Index
Beyond melt and pivot, pandas can reshape using the index itself. The tools are stack and unstack on hierarchical labels. 🏗️
What a MultiIndex Is
A MultiIndex stacks two or more label levels on one axis, so a single row can carry both a region and a year together.
stack Pushes Columns Down
stack moves the innermost column level into the row index, making the table taller and narrower in one move.
df.stack()unstack Pulls Rows Up
unstack is the reverse: it lifts an index level back into columns, making the table wider and shorter again.
df.unstack()They Undo Each Other
Stack then unstack returns the original shape. Think of them as a paired toggle between row labels and column labels.
Pick a Level to Move
Pass a level name or number to choose which one shifts. This level control lets you reshape exactly the axis you mean.
df.unstack(level='year')Selecting From a MultiIndex
Use loc with a tuple to drill into nested labels, grabbing one region-year combination directly from the stacked index.
df.loc[('West', 2021)]Cross-Section With xs
The xs method slices one level cleanly, like every row for year 2021, without unpacking the whole index by hand.
df.xs(2021, level='year')Flatten When You Are Done
To return to plain columns, call reset_index. It turns every index level back into an ordinary, exportable column.
df.reset_index()Stack Drops NaN by Default
By default stack quietly removes missing combinations. Set dropna to False when you need to keep every gap visible.
df.stack(dropna=False)Where This Fits
groupby with several keys returns a MultiIndex, so unstack is the natural finishing move to spread those groups into a grid.
Quick Check
Last check: stacking and unstacking.
Recap: You Can Reshape Anything
You now wield pivot_table, melt, stack, and unstack. With layout under control, your analysis stays clean and flexible. 🎉
Często zadawane pytania
Czy lekcja „stack, unstack i MultiIndex” jest bezpłatna?
Tak — pełny tekst „stack, unstack i MultiIndex” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Data Science Academy, przejdź na CoddyKit PRO. Kurs Data Science Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „stack, unstack i MultiIndex”?
Zmiana kształtu tabel hierarchicznych Ćwiczysz Data Science Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Data Science Academy?
Nie wymagamy żadnego doświadczenia. Data Science Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „stack, unstack i MultiIndex”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Data Science Academy?
Tak. Każda lekcja Data Science Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Układ szeroki i długi oraz dane tidy
- pivot_table do tabel krzyżowych
- melt do przechodzenia do układu długiego
- stack, unstack i MultiIndex