0

i got two df with two similar columns but different names , and i want to concat them by the similar columns

that is my df

data={"col1":["A","B","D","f"],
"col2":[4,2,4,6],
"col3":[7,6,9,11],
"col4":[14,11,22,8],
"multi":[1.4,2.5,1.6,2.2]}
df1=pd.DataFrame.from_dict(data)

data2={"col5":["A","B","D","f"],
"col6":[345,55,44,16],
"col7":[4,7,4,6],
"col8":[1,5,3,9],
"multi_2":[2,2,4,9]}
df2=pd.DataFrame.from_dict(data2)

i want to connect them by columns ["col1","col5"]

and it need to look like this but its can be with "col5" too

data3={"col1":["A","B","D","f"],
"col2":[4,2,4,6],
"col3":[7,6,9,11],
"col4":[14,11,22,8],
"multi":[1.4,2.5,1.6,2.2],
"col6":[345,55,44,16],
"col7":[4,7,4,6],
"col8":[1,5,3,9],
"multi_2":[2,2,4,9]}
df3=pd.DataFrame.from_dict(data3)
df3
matan
  • 407
  • 1
  • 7

1 Answers1

1
df1.merge(df2, left_on='col1', right_on='col5').drop('col5', axis=1)
Tom Ron
  • 4,574
  • 2
  • 14
  • 25