Merging နှင့် Joining
Data analysis မှာ table တစ်ခုတည်းနဲ့မပြီးတာများပါတယ်။ Sales table တစ်ခု၊ product info table တစ်ခု၊ customer table တစ်ခု သီးသန့်ရှိနိုင်ပါတယ်။ pd.merge() က SQL join လိုပဲ common column တစ်ခုကိုအခြေခံပြီး DataFrame နှစ်ခုကိုပေါင်းစပ်ပေးပါတယ်။
python
import pandas as pd
sales = pd.DataFrame({
"ProductID": [1, 2, 1, 3],
"Qty": [3, 2, 1, 4]
})
products = pd.DataFrame({
"ProductID": [1, 2, 3],
"ProductName": ["Tea", "Coffee", "Cake"],
"Price": [1200, 1800, 2500]
})
merged = pd.merge(sales, products, on="ProductID", how="left")
merged["Total"] = merged["Qty"] * merged["Price"]
print(merged)ProductID က table နှစ်ခုစလုံးမှာရှိတဲ့ common key ဖြစ်ပါတယ်။ how="left" က sales table ထဲက rows အားလုံးကိုထားပြီး products table မှ data ကိုလိုက်ဖြည့်တာပါ။ Merge ပြီးမှ price နဲ့ quantity ကိုမြှောက်ပြီး total တွက်ထားပါတယ်။
You should see
ProductID Qty ProductName Price Total 0 1 3 Tea 1200 3600 1 2 2 Coffee 1800 3600 2 1 1 Tea 1200 1200 3 3 4 Cake 2500 10000