stack, unstack และ MultiIndex
ปรับรูปทรงตารางแบบลำดับชั้น
stack, unstack และ MultiIndex เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “stack, unstack และ MultiIndex”
ปรับรูปทรงตารางแบบลำดับชั้น คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “stack, unstack และ MultiIndex” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แบบกว้างกับแบบยาว และข้อมูลเป็นระเบียบ
- pivot_table สำหรับตารางไขว้
- melt เพื่อเปลี่ยนเป็นรูปแบบยาว
- stack, unstack และ MultiIndex