Skip to content Skip to sidebar Skip to footer

Left Join In Pandas Without The Creation Of Left And Right Variables

I'm missing something in the syntax of merging in pandas. I have the following 2 data frames: >>> dfA s_name geo zip date value 0 A002X zip 60601 2010 None 1

Solution 1:

I think you need combine_first with MultiIndex created by set_index:

cols = ["s_name","geo","zip","date"]

df = dfA.set_index(cols).combine_first(dfB.set_index(cols)).reset_index()
print (df)
  s_name  geo    zip  date  value
0  A002X  zip  60601  2010    1.0
1  A002Y  zip  60601  2010    2.0
2  A003X  zip  60601  2010    NaN
3  A003Y  zip  60601  2010    4.0

Or update:

df = dfA.set_index(cols)
df.update(dfB.set_index(cols))
df = df.reset_index()

Post a Comment for "Left Join In Pandas Without The Creation Of Left And Right Variables"