-2

I would like to do a pandas left merge on a key column but only when the key is not null. In SQL:

select * from tb1l left join tbl2 on tbl1.id=tbl2.id and tbl1.id is not null

How can I do in pandas?

Victor
  • 16,609
  • 71
  • 229
  • 409
  • https://stackoverflow.com/help/minimal-reproducible-example – moys Aug 13 '19 at 15:26
  • 1
    Welcome to SO. Unfortunately, SO is not a free code writing service, nor is it a tutorial website. Please review [ask] and create a [mcve] that asks a specific programming question, with a clear sample input/output. Don't forgot to also include your attempt to solve your own problem. – user3483203 Aug 13 '19 at 15:31
  • Possible duplicate of [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – help-ukraine-now Aug 13 '19 at 15:33

1 Answers1

0

I would do it this way:

df = (df_tb1l
      .loc[lambda d: d["id"].notnull(), "id"]
      .merge(df_tbl2, on=["id"], how="left"]
      .merge(df_tb1l, on=["id"], how="right"])
ivallesp
  • 2,018
  • 1
  • 14
  • 21