import pandas as pd
left =pd.DataFrame({ 'A':['A0','A1','A2','A3'],
'B':['B0','B1','B2','B3'],
'key':['K0','K1','K2','K3'],})
right =pd.DataFrame({ 'C':['C0','C1','C2','C3'],
'D':['D0','D1','D2','D3'],
'key':['K0','K1','K2','K3'],})
left
right
merge:合併javascript
pd.merge(left,right)#直接合並,重複的就再也不顯示
pd.merge(left,right,on='key')#以key爲界進行合併
left =pd.DataFrame({ 'A':['A0','A1','A2','A3'],
'B':['B0','B1','B2','B3'],
'key1':['K0','K1','K2','K3'],
'key2':['K0','K1','K2','K3']})
right =pd.DataFrame({ 'C':['C0','C1','C2','C3'],
'D':['D0','D1','D2','D3'],
'key1':['K0','K1','K2','K3'],
'key2':['K0','K1','K2','K3']})
left
right
pd.merge(left,right)#直接合並,重複的就再也不顯示
pd.merge(left,right,on='key1')#以key1爲界進行合併,key2自動分組
pd.merge(left,right,on=['key1','key2'])#以key1,key2爲界進行合併
使key2的值不徹底同樣,right改爲K4css
right =pd.DataFrame({ 'C':['C0','C1','C2','C3'],
'D':['D0','D1','D2','D3'],
'key1':['K0','K1','K2','K3'],
'key2':['K0','K1','K2','K4']})
pd.merge(left,right,on=['key1','key2'])#以key1,key2爲界進行合併,可是key2不一樣的那一行就被刪除
pd.merge(left,right,on=['key1','key2'],how='outer')#how='outer'爲並集,可是默認是交集
pd.merge(left,right,on=['key1','key2'],how='outer',indicator=True)#指定當前的merge是交集仍是並集
pd.merge(left,right,on=['key1','key2'],how='left')#how='left'指定以left爲基準,也能夠指定其餘爲基準