使用join合并,着重关注的是⾏行行的合并
import pandas as pd
df3=pd.DataFrame({'Red':[1,3,5],'Green':[5,0,3]},index=list('abc'))
df4=pd.DataFrame({'Blue':[1,9,8],'Yellow':[6,6,7]},index=list('cde'))
print(df3)
print(df4)
df3.join(df4,how='left')
df3.join(df4,how='right')
df3.join(df4,how='outer')
df5=pd.DataFrame({'Brown':[3,4,5],'White':[1,1,2]},index=list('aed'))
使用merge,着重关注的是列列的合并
df1=pd.DataFrame({'名字':list('ABCDE'),'性别':['男','⼥女女','男','男','⼥女女'],'职称':
['副教授','讲师','助教','教授','助教']},index=range(1001,1006))
df1.columns.name='学院老师'
df1.index.name='编号'
print(df1)
df2=pd.DataFrame({'名字':list('ABDAX'),'课程':['C++','计算机导论','汇编','数据结构','马克思原理理'],'职称':['副教授','讲师','教授','副教授','讲师'] },index=[1001,1002,1004,1001,3001])
df2.columns.name='课程'
df2.index.name='编号'
print(df2)
print(pd.merge(df1,df2))
pd.merge(df1,df2,on='名字',suffixes=['_1','_2'])
pd.merge(df1,df2,how='left')
pd.merge(df1,df2,how='right')
pd.merge(df1,df2,on=['职称','名字'])