1

I'm trying to combine 2 different dataframes (df) horizontally. Both dfs have a unique index value that is the same on both tables.

example of what I have:

**df1**

Name  Job     car  
Peter doctor  Volvo
Tom   plummer 
John  fisher  Honda

**df2**   

Name  Age children
Peter 30    1
Tom   42    3
John  29    5
Mark  26  

What I want

**df3**

Name  Job     car   Age Children 
Peter doctor  Volvo 30   1
Tom   plummer       42   3
John  fisher  Honda 29   5
Mark                26   

Here is what I have so far in my code:

import pandas as pd 

df1 = pd.read_excel('drug.xlsx', index_col=0)
df2 = pd.read_excel('route.xlsx', index_col=0)

df3 = pd.concat([df1, df2], axis=1)

result.to_excel('PythonExport1.xlsx',index=True)
spg719
  • 49
  • 1
  • 5

1 Answers1

0

You can use merge -

df3 = df2.merge(df1, how='left', on='Name')
print(df3)

TO remove NAN -

df3 = df2.merge(df1, how='left', on='Name').fillna('')
Nk03
  • 14,699
  • 2
  • 8
  • 22