pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,
left_index=False, right_index=False, sort=True,
suffixes=('_x', '_y'))
列几个例子细细品味:
import pandas as pd
dataDf1=pd.DataFrame({'列1_left':['a','b','b','c'],
'列2_left':[1,2,2,3]})
dataDf2=pd.DataFrame({'列1_right':['b','c','c','d'],
'列2_right':[2,3,3,4]})
print(dataDf1)
print(dataDf2)
dataLfDf=pd.merge(dataDf1,dataDf2, left_on='列1_left',right_on='列1_right') #默认how='inner'
dataLfDf
dataLfDf=pd.merge(dataDf1,dataDf2, how='outer',left_on='列1_left',right_on='列1_right')
dataLfDf
dataLfDf=pd.merge(dataDf1,dataDf2, how='right',left_on='列1_left',right_on='列1_right')
dataLfDf
import pandas as pd
dataDf1=pd.DataFrame({'key':['a','b','b','c'],
'列2_left':[1,2,2,3]})
dataDf2=pd.DataFrame({'key':['b','c','c','d'],
'列2_right':[2,3,3,4]})
print(dataDf1)
print(dataDf2)
dataLfDf=pd.merge(dataDf1,dataDf2, how='outer',on='key')
dataLfDf
dataLfDf=pd.merge(dataDf1,dataDf2, how='left',on='key')
dataLfDf