python数据表的合并_merge

时间:2022-09-18 20:09:27
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
python数据表的合并_merge

 


df2
python数据表的合并_merge

 

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

 python数据表的合并_merge

 

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

python数据表的合并_merge

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

python数据表的合并_merge

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

python数据表的合并_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")

python数据表的合并_merge