根据 莫烦Python的教程 总结写成,以便自己复习和使用,这里我就不哟林地挂原创了。
on:以列为索引进行合并
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']})
res = pd.merge(left,right,on=['key1','key2'],how='left') # how = inner:党同伐异;outer:求同存异;right:以第二个参数即right的'key1','key2'为准,得到的结果包含所有的'key1','key2'值,left:同理
index:以行为索引进行合并
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')
indicator 显示数据是按照哪种规则合成
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='indicator')
给合并后的数据加上后缀以区分不同的数据
boys = pd.DataFrame({
'k':['K0','K1','K2'],'age':[1,2,3]})
girls = pd.DataFrame({
'k':['K0','K0','K3'],'age':[4,5,6]})
res = pd.merge(boys,girls,on='k',suffixes=['_boy','_girl'],how='inner')