How to do conditional merge Pandas

Viewed 55

I have two different dataframes shown below.

This is the tel_times dataframe

enter image description here

And this is the maint_comp1 dataframe.

enter image description here

Now, I have joined these two dataframes using merge.

maint_tel_comp1 = pd.merge(tel_times, maint_comp1,  how='inner', left_on=['machineID','datetime_tel'], right_on = ['machineID','datetime_maint'])

The result is

enter image description here

1 Answers

I believe what you want is:

maint_tel_comp1 = tel_times.merge(maint_comp1, on='machineID', how='inner')
maint_tel_comp1[maint_tel_comp1['datetime_tel'].gt(maint_tel_comp1['datetime_maint'])]

The problem is your merge was also ensuring datetime_tel == datetime_maint, hence your condition was returning an empty DataFrame.

Related