基于pd.merge函数,实现两个表格的内连接、外连接、左连接和右连接,代码示例如下
import pandas as pd
df1 = pd.DataFrame({'year':[2020,2020,2021,2021,2022],
'company':['apple','google','apple','google','apple'],
'profit':[100,200,150,300,180]})
df1
df2 = pd.DataFrame({'year':[2020,2020,2021,2021,2021],
'company':['apple','google','apple','google','tesla'],
'employee':[20000,50000,22000,60000,30000]})
df2
pd.merge(df1,df2,how='inner',on=['year','company'])
pd.merge(df1,df2,how='outer',on=['year','company'])
pd.merge(df1,df2,how='left',on=['year','company'])
pd.merge(df1,df2,how='right',on=['year','company'])