python combined data table _merge

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) # no designated places on the column name as the intersection of two DataFrame linkages, here it refers to the "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")

 

Guess you like

Origin www.cnblogs.com/yellow-hgy/p/11031060.html