الدمج حسب المفاتيح والفهارس
الربط باستخدام الأعمدة أو الفهرس
الدمج حسب المفاتيح والفهارس درس مجاني في Data Science Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Data Science Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Data Science Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Same Name, One Word
When both tables share a column with the same name, on is all you need to merge them together.
orders.merge(customers, on="customer_id")Join on Many Keys
Sometimes one column is not enough. Pass a list to on and pandas matches rows on every key at once.
sales.merge(targets, on=["region", "month"])Different Names, No Problem
When key columns are named differently, use left_on and right_on to tell pandas which column maps to which.
orders.merge(users, left_on="uid", right_on="id")A Leftover Column
With left_on and right_on, both key columns survive in the result. You often drop the duplicate afterward.
merged.drop(columns="id")The Index Can Be a Key Too
If your key lives in the index instead of a column, set left_index or right_index to True to join on it.
orders.merge(prices, left_on="sku", right_index=True)join for Index Merges
For two index-aligned tables, the join method is shorter. It merges on the index by default.
left.join(right)Handle Overlapping Names
When both tables have a column with the same name, pandas adds suffixes so the two versions stay distinct.
a.merge(b, on="id", suffixes=("_a", "_b"))Keys Must Share a Type
A common surprise: a key stored as a string on one side and an integer on the other will simply never match.
Mind Duplicate Keys
If a key value repeats on both sides, merge produces every matching pair. Rows can multiply unexpectedly.
join Defaults to Left
Unlike merge, the join method uses a left join by default. Pass how to change it when you need to.
left.join(right, how="outer")Pick the Right Tool
Use merge for column keys with full control, and join when both tables are already aligned on their index. 🔧
Quick Check
Your key columns have different names in each table. What do you use?
Recap: Matching the Keys
You can merge on shared columns, mismatched names, or the index, and use suffixes to keep overlapping columns clear. Nicely done!
الأسئلة الشائعة
هل درس «الدمج حسب المفاتيح والفهارس» مجاني؟
نعم — نص درس «الدمج حسب المفاتيح والفهارس» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Data Science Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Data Science Academy 4 دروس في المجموع.
ماذا ستتعلم في «الدمج حسب المفاتيح والفهارس»؟
الربط باستخدام الأعمدة أو الفهرس تتمرن على Data Science Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Data Science Academy؟
لا تُشترط خبرة سابقة. Data Science Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الدمج حسب المفاتيح والفهارس»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Data Science Academy هذا؟
نعم. كل درس في Data Science Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الربط الداخلي والأيسر والأيمن والخارجي
- الدمج حسب المفاتيح والفهارس
- استخدام concat للتكديس والإلحاق
- تشخيص عمليات الربط الخاطئة