์์ ๋ฐ์ดํฐ ํ๋ ์
import pandas as pd
left = pd.DataFrame({
'id':[1,2,3,4,5],
'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
'subject_id':['sub1','sub2','sub4','sub6','sub5']})
right = pd.DataFrame(
{'id':[1,2,3,4,5],
'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
'subject_id':['sub2','sub4','sub3','sub6','sub5']})
1. ๋ ๊ฐ์ ๋ฐ์ดํฐํ๋ ์์ Key ๊ธฐ์ค์ผ๋ก ํฉ์น๊ธฐ
pd.merge(left,right,on='id')
2. ๋ ๊ฐ์ ๋ฐ์ดํฐํ๋ ์์ multiple key๋ก ํฉ์น๊ธฐ
pd.merge(left,right,on=['id','subject_id'])
3. merge์ how ์ธ์ ์ถ๊ฐํด์ ํฉ์น๊ธฐ
Merge ๋ฐฉ๋ฒ | SQL์์ ? | ์ค๋ช |
left | LEFT OUTER JOIN | ์ผ์ชฝ ๋ฐ์ดํฐํ๋ ์ key๋ก ํฉ์น๊ธฐ |
right | RIGHT OUTER JOIN | ์ค๋ฅธ์ชฝ ๋ฐ์ดํฐํ๋ ์ key๋ก ํฉ์น๊ธฐ |
outer | FULL OUTER JOIN | key์ ํฉ์งํฉ ์ด์ฉ |
inner | INNER JOIN | key์ ๊ต์งํฉ ์ด์ฉ |
Left join
pd.merge(left, right, on='subject_id', how='left')
Right join
pd.merge(left, right, on='subject_id', how='right')
Outer join
print pd.merge(left, right, how='outer', on='subject_id')
Inner join
pd.merge(left, right, on='subject_id', how='inner')
4. ๋ ๊ฐ์ ๋ฐ์ดํฐํ๋ ์์ concat์ผ๋ก ์ฐ๊ฒฐํ๊ธฐ (ํ ์๋๋ก ์ฝ์ )
pd.concat([left, right])
728x90