jm1701
jm1701

Reputation: 61

Merge dataframes on same row

I have a python code that gets links from a dataframe (df1) , collect data from a website and return output in a new dataframe

df1:

id   Name      link             Country        Continent  
1    Company1  www.link1.com    France         Europe
2    Company2  www.link2.com    France         Europe
3    Company3  www.Link3.com    France         Europe

The ouput from the code is df2:

link           numberOfPPL      City  
www.link1.com       8            Paris
www.link1.com       9            Paris
www.link2.com       15           Paris
www.link2.com       1            Paris

I want to join these 2 dataframes in one (dfinal). My code:

dfinal = df1.append(df2, ignore_index=True)

I got dfinal:

    link           numberOfPPL      City       id   Name     Country  Continent
   www.link1.com       8            Paris
   www.link1.com       9            Paris
   www.link2.com       15           Paris
   www.link2.com       1            Paris
   www.link1.com                               1    Company1  France   Continent
   ..
   ..

I Want my final dataframe to be like this:

    link           numberOfPPL      City       id   Name     Country  Continent
   www.link1.com       8            Paris      1    Company1  France  Europe
   www.link1.com       9            Paris      1    Company1  France  Europe
   www.link2.com       15           Paris      1    Company1  France  Europe
   www.link2.com       1            Paris      2    Company2  France  Europe

Can anyone help please ??

Upvotes: 0

Views: 44

Answers (1)

Mit
Mit

Reputation: 716

You can merge the two dataframes on 'link':

outputDF = df2.merge(df1, how='left', on=['link'])

Upvotes: 2

Related Questions