التجميع حسب عدة مفاتيح
التجميع متعدد المستويات وفهرس النتائج
التجميع حسب عدة مفاتيح درس مجاني في Data Science Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Data Science Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Data Science Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Group on More Than One Column
Pass a list of keys to groupby and pandas forms a group for every unique combination of those columns.
df.groupby(["city", "year"])Every Combo Is a Group
Two cities and three years can yield up to six groups, one per existing combination found in your data.
Aggregate Across Keys
Aggregation works exactly the same; the math just runs inside each combination instead of a single key.
df.groupby(["city", "year"])["sales"].sum()Meet the MultiIndex
Grouping on several keys gives the result a MultiIndex, with one index level for each grouping column.
Read a MultiIndex Result
Each output row is labeled by a tuple like (city, year), so the index tells you exactly which group it is.
Select From a MultiIndex
Use loc with a tuple to pull one specific combination straight out of the grouped result.
result.loc[("Paris", 2024)]Flatten With reset_index
Prefer flat columns? reset_index turns each index level back into an ordinary column you can filter.
result.reset_index()Keep Empty Groups Out
By default pandas skips combinations with no rows, so your result shows only combinations that actually occur.
Order of Keys Matters
The key order you list sets the index level order, which changes how the result sorts and reads.
df.groupby(["year", "city"]).size()Unstack for a Cross-Tab
Call unstack to lift one key into columns, turning a long grouped result into a readable grid.
result.unstack()When to Use Several Keys
Reach for multi-key grouping whenever a question mentions two dimensions, like sales by city and by month.
Quick Check
You group by ["city", "year"]. What labels each result row?
Recap: Two Dimensions, One groupby
You can now slice data along several keys at once, read the MultiIndex, and reshape it for clear cross-tabs. 📊
الأسئلة الشائعة
هل درس «التجميع حسب عدة مفاتيح» مجاني؟
نعم — نص درس «التجميع حسب عدة مفاتيح» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Data Science Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Data Science Academy 4 دروس في المجموع.
ماذا ستتعلم في «التجميع حسب عدة مفاتيح»؟
التجميع متعدد المستويات وفهرس النتائج تتمرن على Data Science Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Data Science Academy؟
لا تُشترط خبرة سابقة. Data Science Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «التجميع حسب عدة مفاتيح»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Data Science Academy هذا؟
نعم. كل درس في Data Science Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- شرح Split-Apply-Combine
- تجميعات متعددة باستخدام agg
- التجميع حسب عدة مفاتيح
- استخدام transform لإنشاء ميزات حسب المجموعة