0

I have two pandas dataframes. One looks like this:

name   source   start    stop   ID
OTU1   ST1      55       321    TQWEOIU_1
OTU1   ST1      88       150    KCIAKLS_2

the second looks like this:

name    group   source   start   stop
OTU1    G1      ST1      55      321
OTU1    G2      ST1      88      150

I want to merge these dataframes so they look like this:

name   group   source   start   stop   ID
OTU1   G1      ST1      55      321    TQWEOIU_1
OTU1   G2      ST1      88      150    KCIAKLS_2

So to summarize I want to merge the dataframe rows based on the condition that the start, stop, name, and source columns are identical. I haven't found a great way to do this so any help is appreciated.

Also if there isn't a way to do this in pandas if any one has advice on other pythonic ways to do this I'd accept those answers as well.

Thanks

eric
  • 127
  • 8

1 Answers1

1

You want pd.merge:

df3 = df2.merge(df1, on=["name", "source", "start", "stop"])
>>> df3
   name group source  start  stop         ID
0  OTU1    G1    ST1     55   321  TQWEOIU_1
1  OTU1    G2    ST1     88   150  KCIAKLS_2
not_speshal
  • 22,093
  • 2
  • 15
  • 30