Home > Software design >  Match two datasets on two columns in python (date being one of these values)
Match two datasets on two columns in python (date being one of these values)

Time:01-14

I have a two datasets where I would like to match on the date and a specific column.

Data

df1

label   date        type1   stat1
111     7/1/2021    y      n
222     8/1/2021    y      n
333     9/1/2021    n      y

df2

  id   date_1      type   stat
  111  7/1/2021    y      n
  222  7/20/2021   y      n
  333  7/30/2021   n      y

Desired

id   date        type   stat   date_1      label     type1   stat1
111  7/1/2021    y      n      7/1/2021    111       y       n
222  8/1/2021    y      n
333  9/1/2021    n      y

Doing

I figure I can treat as a string and do a left join. How would I join on
date value as well as the type column? Any insight is appreciated.

df4 = df1.merge(df2, how='inner', left_on=["date","type1"], right_on=["date_1", "type"])

Although the dates are true dates

CodePudding user response:

then it should be like this :

df3 = df1.merge(server, left_on=['date','type1'], right_on=['date_1','type'], how='left')

output:

>>>label      date type1 stat1     id    date_1 type stat
0    111  7/1/2021     y     n  111.0  7/1/2021    y    n
1    222  8/1/2021     y     n    NaN       NaN  NaN  NaN
2    333  9/1/2021     n     y    NaN       NaN  NaN  NaN
  •  Tags:  
  • Related