1.导入pandas
import pandas as pd
2.定义DataFrame
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','D3']})
left
right
3.介绍merge()方法的用法
pd.merge(left,right)
pd.merge(left,right,on='key')
left = pd.DataFrame({'key1':['K0','K1','K2','K3'],
'key2':['K0','K1','K2','K3'],
'A':['A0','A1','A2','A3'],
'B':['B0','B1','B2','B3']})
right = pd.DataFrame({'key1':['K0','K1','K2','K3'],
'key2':['K0','K1','K2','K4'],
'C':['C0','C1','C2','C3'],
'D':['D0','D1','D2','D3']})
left
right
合并融合key1
pd.merge(left,right,on='key1')
合并融合key1和key2
pd.merge(left,right,on=['key1','key2'])
因为两个DataFrame中的key2特征值不同,merge之后直接把第三行都舍去,取交集
取并集
pd.merge(left,right,on=['key1','key2'],how='outer')
增加指示器
pd.merge(left,right,on=['key1','key2'],how='outer',indicator = True)
以某一个DataFrame为标准与另外一个DataFrame进行merge,舍去第二个DataFrame中与第一个DataFrame不同的行中的内容
pd.merge(left,right,on=['key1','key2'],how='left')
pd.merge(left,right,on=['key1','key2'],how='right')