python数据表的合并_merge

Ref:https://nbviewer.jupyter.org/github/pydata/pydata-book/blob/2nd-edition/ch08.ipynb
import pandas as pd
df1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'], 'data1': range(7)}) df2 = pd.DataFrame({'key': ['a', 'b', 'd'], 'data2': range(3)}) df1

 


df2

 

 
pd.merge(df1, df2) #不指定on则以两个DataFrame的列名交集做为连接键 ,这里指的是"key"
pd.merge(df1,df2,on = "key")

 

 

pd.merge(df1,df2,on = "key",how = "left")  #左连接

pd.merge(df1,df2,on = "key",how = "right")  #右连接

df2.columns=["key2","data2"] #df2 改名
df2
pd.merge(df1,df2,left_on="key",right_on="key2")  ##当左右两个DataFrame的列名不同,指定不同的列明进行merge

 

 

#同样可以进行左外连接,右外连接,内链接
pd.merge(df1,df2,left_on="key",right_on="key2",how = "left")
pd.merge(df1,df2,left_on="key",right_on="key2",how = "right")
pd.merge(df1,df2,left_on="key",right_on="key2",how = "inner")

 

posted @ 2019-06-16 12:15  Yellow_huang  阅读(29483)  评论(0编辑  收藏  举报