stack وunstack وMultiIndex
إعادة تشكيل الجداول الهرمية
stack وunstack وMultiIndex درس مجاني في Data Science Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Data Science Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Data Science Academy 4 دروس في المجموع.
ماذا ستتعلم في «stack وunstack وMultiIndex»؟
إعادة تشكيل الجداول الهرمية تتمرن على Data Science Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Data Science Academy؟
لا تُشترط خبرة سابقة. Data Science Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «stack وunstack وMultiIndex»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Data Science Academy هذا؟
نعم. كل درس في Data Science Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- البيانات العريضة والطويلة والمنظمة
- استخدام pivot_table للجداول التقاطعية
- استخدام melt للتحويل إلى الشكل الطويل
- stack وunstack وMultiIndex