1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| from turtle import right import pandas as pd import numpy as np
left = pd.DataFrame({ 'key':['K0','K1','K2','K3'], 'A':['A0','A1','A2','A3'], 'B':['B0','B1','B2','B3'] }) right = pd.DataFrame({ 'key':['K0','K1','K2','K3'], 'C':['C0','C1','C2','C3'], 'D':['D0','D1','D2','C3'] }) print(left) print(right)
res = pd.merge(left,right,on = 'key') print(res)
print("************--1--***************") left = pd.DataFrame({ 'key1':['K0','K0','K1','K2'], 'key2':['K0','K1','K0','K1'], 'A':['A0','A1','A2','A3'], 'B':['B0','B1','B2','B3'] }) right = pd.DataFrame({ 'key1':['K0','K1','K1','K2'], 'key2':['K0','K0','K0','K0'], 'C':['C0','C1','C2','C3'], 'D':['D0','D1','D2','C3'] })
res = pd.merge(left,right,on=['key1','key2'],how='inner') print(res)
res = pd.merge(left,right,on=['key1','key2'],how='outer') print("**********--outer--**********") print(res)
res = pd.merge(left,right,on=['key1','key2'],how='left') print("*********--left--**********") print(res)
df1 = pd.DataFrame({ 'col1':[0,1], 'col_left':['a','b'] }) df2 = pd.DataFrame({ 'col1':[1,2,2], 'col_right':[2,2,2] })
res = pd.merge(df1,df2,on='col1',how='outer',indicator=True) print(res)
left = pd.DataFrame({ 'A':['A0','A1','A2'], 'B':['B0','B1','B2'] },index = ['K0','K1','K2']) right = pd.DataFrame({ 'C':['C0','C1','C2'], 'D':['D0','D1','D2'] },index = ['K0','K2','K3']) res = pd.merge(left,right,left_index=True,right_index=True,how='outer') print("*********--index--**********") print(res)
boys = pd.DataFrame({ 'k':['K0','K1','K2'], 'age':[1,2,3] }) girls = pd.DataFrame({ 'k':['K0','K0','K3'], 'age':[4,5,6] }) print(boys) print(girls)
res = pd.merge(boys,girls,on = 'k',how = 'outer',suffixes=['_boy','_girl']) print(res)
|