收到一只叮咚
与内部连接一起使用DataFrame.merge,只有必要的重命名列:df = df2.rename(columns={'col1':'Col1','col2':'Col2'}).merge(df1, on=['Col1','Col2'])#on should be omited, then merge by intersection of columns of df1, df2#df = df2.rename(columns={'col1':'Col1','col2':'Col2'}).merge(df1)print (df) Col1 Col2 Col30 A 4 ab1 B 3 jd另一个想法是使用left_on和right_on参数,然后删除具有名称的列df2.columns:df = (df2.merge(df1, left_on=['col1','col2'], right_on=['Col1','Col2']).drop(df2.columns, axis=1))print (df) Col1 Col2 Col30 A 4 ab1 B 3 jd如果列名相同:print (df2) Col1 Col21 A 42 B 3df = df2.merge(df1, on=['Col1','Col2'])#df = df2.merge(df1)print (df) Col1 Col2 Col30 A 4 ab1 B 3 jd