1

Working with a +450K rows x 90 col data frame, I need to perform several pd.merge (Excel´s "vlookup") commands.

In contrast with existing solutions, I think that a new data frame with the mentioned dimensions for each merge isn't efficient.

How can I merge df1 with df2 on a new column inside df1?

So far my approach was to point the merge to a new column inside df1. without success.

import pandas as pd

df1=pd.DataFrame({'Campo':['C1','C2','C3'],'Plataformas':['C1-A','C2-A','C3-A']})

df2=pd.DataFrame({'Plataformas':['C1-A','C2-A','C3-A'],'Pozos':[5,10,15]})

df3=df1.merge(df2,on='Plataformas')   #This works for the df3 

t1['Num Pozos']=t1.merge(t2,on='Plataformas')  #This didn't worked

DF1
Campo   Plataformas
C1  C1-A
C2  C2-A
C3  C3-A

DF2
Plataformas Pozos
C1-A            5
C2-A            10
C3-A            15

New DF1 (Expected)
Campo   Plataformas Pozos
C1  C1-A             5
C2  C2-A             10
C3  C3-A             15

To avoid df3 I tried:

df1['Num Pozos']=df1.merge(df2,on='Plataformas')

The output error was:

ValueError: Wrong number of items passed 4, placement implies 1

Any help is appreciated.

--- Edited question in response to @coldspeed --- In my opinion, the difference between similar questions is because of the other answers implies to create a new data frame which is not necessary in my case.

Atlahua
  • 23
  • 5
  • Not sure what you need ,but you may can check with https://stackoverflow.com/questions/53645882/pandas-merging-101 – BENY Mar 28 '19 at 01:54
  • 4
    To merge a single column, use `map`: `df1['New'] = df1['Plataformas'].map(df2.set_index('Plataformas')['Pozos'])` (it's also in the link above) You can then do this for every DataFrame. Although if you're merging multiple dataframes, you might want `pd.concat` using "Plataformas" as the index (also in the link) – cs95 Mar 28 '19 at 01:55
  • `df1['Num Pozos'] = df1.merge(df2,on='Plataformas')['Pozos']` – It_is_Chris Mar 28 '19 at 01:55
  • Thank you all. ``` df1['Num Pozos'] = df1.merge(df2,on='Plataformas')['Pozos'] ``` solved my question. Still i'll take a look for the other solutions. Thanks a lot @Chris! – Atlahua Mar 28 '19 at 15:06

0 Answers0