stack、unstack、MultiIndex
階層型テーブルの形を変える
「stack、unstack、MultiIndex」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎉
よくある質問
「stack、unstack、MultiIndex」レッスンは無料ですか?
はい。「stack、unstack、MultiIndex」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。
「stack、unstack、MultiIndex」で何を学びますか?
階層型テーブルの形を変える ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Data Science Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「stack、unstack、MultiIndex」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このData Science Academyレッスンでコードを書いて実行できますか?
はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ワイド型とロング型、Tidyデータ
- クロス集計にpivot_tableを使う
- meltでロング型に変換する
- stack、unstack、MultiIndex